Continue Program execution even after try catch

26,296

Solution 1

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
{
}

Solution 2

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.

Solution 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?

Solution 4

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.

Share:
26,296
SKumar
Author by

SKumar

Updated on January 08, 2020

Comments

  • SKumar
    SKumar over 4 years

    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?