Handling "exception errors"

  • Thread starter Thread starter mikeb
  • Start date Start date
M

mikeb

I'm trying to find the correct (?) way to capture errors and display them
appropriately.

Try
'some code here
Catch ex as exception
msgbox (ex.message)
Finally
'some final code here
End Try

With SQLce errors I add 'Catch ex as SQLCeException"

Thats an easy one to figure out, but how do I best catch exceptions to other
types of possible errors. How does the developer best plan for these?

Were bumping up against some errors, that no matter what *exception type we
use in the catch, the code still catches in the final Catch ex as Exception
line (we put it last in the Catch list too).

Can you help me out?

Thanks,

Mike
 
The exceptions that get thrown will depend on what code is being run in 'some
code here. Your best approach would be to check the documentation for the
code you are calling (for example in the msdn library) and it should indicate
different exceptions that might be thrown. If it is impossible to determine
from documentation what exceptions might be thrown you can also try running
the code in situations where you expect it to fail with a debugger attached.
Then you can inspect any thrown exception to see what type it is and add
catch clauses to handle these cases.

Last there are likely a set of exceptions which are hard to anticipate and
very unlikely to have any way to easily correct the underlying issue. For
these types of issues you may have to resort to catching all exceptions. Once
you have an ambiguous exception about all you can do is either let the user
know something went wrong and/or throw it again so that code farther up the
stack might be able to handle the error.

-Noah Falk
 
NoahFalk said:
Last there are likely a set of exceptions which are hard to
anticipate and very unlikely to have any way to easily correct the
underlying issue. For these types of issues you may have to resort to
catching all exceptions. Once you have an ambiguous exception about
all you can do is either let the user know something went wrong
and/or throw it again so that code farther up the stack might be able
to handle the error.

I'm generally of the school of thought that if you run across an
exception/error you are not prepared to handle, you should let it bubble up
to the caller (not a re-throw). I say "generally" because it still causes
me some apprehension at times ("What happens if the caller isn't prepared to
handle exceptions?").

I guess the bottom line is "trial and error" may be in order here.
Obviously, you should catch the exceptions you know you can handle. Whether
to re-throw the rest or simply let them bubble up is the open question.

--
John T
http://tknowlogy.com/TknoFlyer
http://www.pocketgear.com/products_search.asp?developerid=4415
Reduce spam. Use Sender Policy Framework: http://spf.pobox.com
____________________
 
Back
Top