ADO DOT NET Posted February 15, 2007 Posted February 15, 2007 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? Quote
NeuralJack Posted February 15, 2007 Posted February 15, 2007 (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 February 15, 2007 by NeuralJack Quote Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
ADO DOT NET Posted February 15, 2007 Author Posted February 15, 2007 So Catch Else is not possible? Quote
Administrators PlausiblyDamp Posted February 15, 2007 Administrators Posted February 15, 2007 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 :-\ Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
ADO DOT NET Posted February 15, 2007 Author Posted February 15, 2007 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 Quote
MrPaul Posted February 15, 2007 Posted February 15, 2007 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 :) Quote Never trouble another for what you can do for yourself.
Administrators PlausiblyDamp Posted February 15, 2007 Administrators Posted February 15, 2007 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.