471,075 Members | 791 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,075 software developers and data experts.

How would you approach this using Structured Exception Handling?

in the vb.old days we used to be able to do something like

\\\
sub
on local error goto typeMismatchHandler
_someIntegerVariable = _someVariableThatMightBeNothingOrNull
_anotherIntegerVariable = _anotherVariableThatMightBeNothingOrNull
_yetAnotherIntegerVariable =
_yetAnotherVariableThatMightBeNothingOrNull
exit sub

typeMismatchHandler:
if err = TypeMismatch then
'if it tries to set it to nothing, i don't care - handle silently
resume next
else
'handle real errors
end if
exit sub
end sub
///
off the top of my head, the first analogous approach i come up with using
SEH is:
\\\
sub
try
_someIntegerVariable = _someVariableThatMightBeNothingOrNull
catch ex as System.InvalidCastException
'don't care, handle silently
end catch

try
_anotherIntegerVariable = _anotherVariableThatMightBeNothingOrNull
catch ex as System.InvalidCastException
'don't care, handle silently
end catch

try
_yetAnotherIntegerVariable =
_yetAnotherVariableThatMightBeNothingOrNull
catch ex as System.InvalidCastException
'don't care, handle silently
end catch
end sub
///

....which in this trivial example is not that bad, but in my real program,
there are many more lines than just 3. i am trying to figure out a more
'elegant' approach to try to keep the code readable. (otherwise this sub
will be a mile long!)

....second thought off the top of my head would be to stick variables and
values in a arrays and loop through them - keeping the try-catch block in a
loop would keep things much more readable, and i'm going to fool around with
that as soon as i post this msg, but...

i guess what i'm really looking for here is try-catch's functional
equivalent of 'Resume Next'...

anyone have any ideas? approaches?

as always, any insight would be much appreciated!

TIA! =)
Jul 21 '05 #1
1 1045

"lover" <microsoft rulez!> wrote in message
news:uW**************@TK2MSFTNGP09.phx.gbl...
in the vb.old days we used to be able to do something like

First, trying to do a
Dim var1 as SomeClass = CType(SomeClass,nothing)
'Sorry, not VB man

Is perfectly legal and result in var1 = nothing, so you wouldn't get an
exception.
Second, just expend the try block:

try

_someIntegerVariable = _someVariableThatMightBeNothingOrNull
_anotherIntegerVariable = _anotherVariableThatMightBeNothingOrNull
yetAnotherIntegerVariable = _yetAnotherVariableThatMightBeNothingOrNull
catch ex as System.InvalidCastException
'don't care, handle silently
end catch

Will work just as well.
Jul 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by A. Saksena | last post: by
18 posts views Thread by Digital Puer | last post: by
1 post views Thread by Anonieko | last post: by
3 posts views Thread by Bruce One | last post: by
23 posts views Thread by marora | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.