When I use try, catch blocks, if any exception is throws the program execution is stopped after the catch is handled. But, I need to continue the program execution even if there is exception. Can any one help me how to do it?
- 2Please provide a code sample, and consider looking up the use of FinallyPeter T. LaComb Jr.– Peter T. LaComb Jr.2010-12-29 19:54:09 +00:00Commented Dec 29, 2010 at 19:54
- 3When you say "I need to continue the program...", continue from where, exactly? From the statement where the exception was thrown, or after the try/catch block, or...?Daniel Pratt– Daniel Pratt2010-12-29 19:56:13 +00:00Commented Dec 29, 2010 at 19:56
5 Answers
If I understand correctly, here's what you're wanting:
try { Statement1(); // <-- Exception is thrown in here Statement2(); // <-- You want to go here after the catch block executes } catch { HandleException(); } Try/catch blocks don't work that way. You would have to rewrite your code as follows, instead:
try { Statement1(); } catch { } try { Statement2(); } catch { } Comments
Uncaught exceptions terminate execution.
If an exception is caught and not rethrown, the catch() clause is executed, then the finally() clause (if there is one) and execution then continues with the statement following the try/catch/finally block.
If an exception is caught and rethrown, the catch() clause is executed up to and including the throw statement; the finally() clause (if there is one) is executed), then exception is (re-)thrown and the stack unwinding continues.
As the call stack is unwound, finally() clauses are executed as they go out of scope and Dispose() is called as variables declare in using statements go out of scope.
What does not happen is that control does not (and cannot) resume at the point the original exception was thrown. It sounds like you are catching exceptions at a high level -- such as your Main() method -- and expecting execution to continue at original point of failure.
To make that happen, you need to catch the exception at the point at which handling makes contextual sense, and, having handled the exception, either retry the failing operation or ignore the problem.
Doing exception handling well is rather difficult; hence the dictum that the best exception handling practice is to not handle it. Exceptions are supposed to be just that: exceptional. Your code should not throw exception as a normal matter of course; nor should you generally use exceptions as validation technique or as a flow-of-control operator.
3 Comments
catch (Exception e) only catches exceptions derived from System.Exception. The latter catches all exceptions including exceptions not-derived from System.Exception (and yes, .Net allows throwing such CLS exceptions), although C# does not. learn.microsoft.com/en-us/dotnet/csharp/how-to/…If you handle the exception and do not re-throw it (or another Exception) from your catch block, your program should resume.
Additionally, if you are catching exceptions of a certain type (say IO exceptions), but the code in the try block is throwing a different type (say a SQL exception), your catch block with not catch it and the exception will bubble up till the program terminates.
What exactly are you doing in your catch blocks?
Comments
If you've reached out to a method that contains your try and catch, you could just do something like this...
//Start Here exceptionMethod() //Code will continue here after you've returned from your catch block in exceptionMethod() doSomeMoreStuff() exceptionMethod() try{ doStuff() } catch(Exception e){ return } a simple return in your catch block should do the trick.