Basic Try Catch question

ADO DOT NET

Centurion
Joined
Dec 20, 2006
Messages
160
Hello,
Visual Basic:
Try

Catch x1 As x1

Cacth x2 As x2

End Try
I just want to know that if in the above code another exception occurs how can I catch it?
In other words, I want to catch all other exceptions at once.
Do we have something like, Catch Else?
 
This should do it.. well i'm not sure about Ado.Net but it works in vb.net. There should be something similar

Code:
Try

Catch x1 As x1

Catch x2 As x2

Catch ex as Exception

End Try
 
Last edited:
Often catching all exceptions can be considered a bad practice; when you are implementing a catch block you are assuming responsibility for handling the problem correctly. If you are unaware of the exact type of problem it is very difficult to decide how to deal with it.

In some situations it may be impossible for you to handle it anyway (try catching an OutOfMemoryException or StackOverflowException for example).

Catching all exceptions while developing or debugging can hide potential problems - both from you or from parts of code higher up the chain that can handle them correctly, this is potentially introducing new problems rather than solving them.

Without knowing why you need to catch all exceptions it's hard to give definite rules though, so my above post may not be relevant :-\
 
In my real code I am catching about 50 exceptions!
Just want to catch all the rest that I am not aware of them and may occur!
So still can use Catch Else As What?! :D
 
Exception class

All exceptions derive from Exception, so a catch block which catches type Exception will catch all other (unhandled) exceptions, as in NeuralJack's code.

However, I agree with PlausiblyDamp that you should avoid code which catches any exception, as your program cannot possibly know how to deal with it correctly. If you do catch it (for an error log, for example), then you should re-throw it, or throw another exception, to alert methods up the call stack.

Good luck :)
 
50 different exceptions? Is this all in one place? If so are they all thrown by a 3rd party library?

Where possible you should be catching the exceptions as close to where they occur as you can.

It might help if you gave a bit more detail about your situation to see if any further advice can help.
 
Back
Top