Type of error

A

Alex

Hi,


is it possible to determine exactly what type of error/exception was thrown
from a try catch block bt simple catching the generic error.

by this I mean, suppose I have the follwoing code

try
'do some database stuff
catch err as exception
'err now contains my error
end try

The most light error that accessing a db would throw is of type
System.Data.OleDb.OleDbException.
Do I need to specifically look for this type of excepotion in a catch block

try

catch err as System.Data.OleDb.OleDbException

catch err as exception
end try

or can I specifically tell from catching a generic error that it is of type
System.Data.OleDb.OleDbException.
I've noticed that certain methods can throw many types of exception so is to
best to have a catch handler for each type, or just have a generic catch
hanler and then from the object that is caught determine the type of
exception that was thrown.

cheers

alex.
 
K

Kevin Spencer

Good questions, Alex. As you've noticed, there are a large variety of types
of Exceptions. All Exceptions inherit System.Exception, so you can catch all
of them by putting a Catch block in for System.Exception. The .Net SDK does
a wonderful job of documenting the various types of Exceptions that can be
raised by the CLR classes, and it is often a good idea to include multiple
Catch blocks to catch the various types of Exceptions that may be thrown by
a certain operation, so that you can deal effectively with each one. Some
Exceptions can be recovered from in a number of ways, and knowing which type
you are dealing with can help you to write more effective Exception-handling
code. You can also use Reflection to discover what type of Exception was
thrown, and drill down into the InnerException of some Exceptions to find
out what Inner Exception caused the current Exception to be thrown.
Exceptions often cascade like this, and particularly when dealing with
database operations you may find an Inner Exception that was the original
cause of the problem.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Big Things are made up of
Lots of Little Things.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top