SKumar
SKumar

Reputation: 1227

Continue Program execution even after try catch

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?

Upvotes: 3

Views: 42320

Answers (5)

Austin Duran
Austin Duran

Reputation: 103

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.

Upvotes: 0

Nicholas Carey
Nicholas Carey

Reputation: 74197

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.

Upvotes: 10

Saeed Amiri
Saeed Amiri

Reputation: 22555

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.

Upvotes: 2

Jacob
Jacob

Reputation: 78850

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

Upvotes: 11

Oded
Oded

Reputation: 498942

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?

Upvotes: 3

Related Questions