How would you approach this using Structured Exception Handling?

  • Thread starter Thread starter lover
  • Start date Start date
L

lover

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! =)
 
lover said:
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.
 
Back
Top