3

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?

2
  • 2
    Please provide a code sample, and consider looking up the use of Finally Commented Dec 29, 2010 at 19:54
  • 3
    When 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...? Commented Dec 29, 2010 at 19:56

5 Answers 5

11

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 { } 
Sign up to request clarification or add additional context in comments.

Comments

10

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

Yes, indeed. Trick question: what's the difference between the following two try/catch blocks? try { ... } catch( Exception e ) { ... } and try { ... } catch { ... }
Is something wrong with your eyes? :) The second catch has no argument. It catches all but you can't be sure to correctly recover everything and I don't know an easy way to get information regarding the error that occurred.
@TheincredibleJan — 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/…
3

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

2

If you talking about function (not program) you can use finally to continue your function

try { } catch(MyException ex) { } finally { // other code to be done } 

but if you saying program crashes, the cach without any argument can handle it.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.