Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello,

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?

Posted (edited)

This should do it.. well i'm not sure about Ado.Net but it works in vb.net. There should be something similar

 

Try

Catch x1 As x1

Catch x2 As x2

Catch ex as Exception

End Try

Edited by NeuralJack
Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
  • Administrators
Posted

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 :-\

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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 :)

Never trouble another for what you can do for yourself.
  • Administrators
Posted

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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...