Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resuming execution of code after exception is thrown and caught

How is it possible to resume code execution after an exception is thrown?

For example, take the following code:

namespace ConsoleApplication1 {     public class Test     {         public void s()         {             throw new NotSupportedException();             string @class = "" ;             Console.WriteLine(@class);             Console.ReadLine();         }     }      public class Program     {         public static void Main(string[] args)         {             try             {                 new Test().s();             }             catch (ArgumentException x)             {             }             catch (Exception ex)             {             }         }     } } 

After catching the exception when stepping through, the program will stop running. How can I still carry on execution?

EDIT: What I specifically mean is the line Console.WriteLine(@class); does not seem to be hit, because when I run to it when in debug mode, the program exits from debug mode. I want to run to this line and stop at it.

Thanks

like image 352
GurdeepS Avatar asked Jun 09 '10 16:06

GurdeepS


People also ask

How do you continue a program execution even after throwing an exception?

Resuming the program When a checked/compile time exception occurs you can resume the program by handling it using try-catch blocks. Using these you can display your own message or display the exception message after execution of the complete program.

Where does execution resume after an exception has been thrown and caught?

the execution resumes where the exception is caught, that is at the beginning of the catch block which specifically address the current exception type. the catch block is executed, the other catch blocks are ignored (think of multiple catch block as a switch statement).

How do you continue after catching an exception?

The continue statement will restart the loop (as opposed to the break statement, which terminates the loop). As such if you replace break; with continue; , you will keep on looping after your Exception is caught (providing no other Exception is thrown but the one caught), ans the error message is displayed.

Is it possible to resume Java execution after exception occurs?

No. You cannot just jump back into the middle of a method.


2 Answers

Well, you don't have any code after the catch blocks, so the program would stop running. Not sure what you're trying to do.

The following should be proof that the program doesn't simply "stop" after the catch blocks. It will execute code after the catch blocks if there is code to be executed:

static void Main(string[] args) {     try     {         new Test().s();     }     catch (ArgumentException x)     {         Console.WriteLine("ArgumentException caught!");     }     catch (Exception ex)      {          Console.WriteLine("Exception caught!");     }      Console.WriteLine("I am some code that's running after the exception!"); } 

The code will print the appropriate string depending on the exception that was caught. Then, it will print I am some code that's running after the exception! at the end.

UPDATE

In your edit you asked why Console.WriteLine(@class); does not seem to be hit. The reason is that you are explicitly throwing an exception in the very first line of your s() method; anything that follows is ignored. When an exception is encountered, execution stops and the exception is propagated up the call stack until the appropriate handler can handle it (this may be a catch block that corresponds to the try that wraps the statement in question within the same method, or it may be a catch block further up the call-stack. If no appropriate handler is found, the program will terminate with a stacktrace [at least in Java - not sure if the same happens in C#]).

If you want to hit the Console.WriteLine line, then you shouldn't be explicitly throwing an exception at the beginning of the method.

like image 124
Vivin Paliath Avatar answered Oct 07 '22 14:10

Vivin Paliath


It sounds like you're wanting resumeable exceptions. C# doesn't do resumeable exceptions, and I'm doubtful that CLR supports them.

The purpose of throwing an exception is to abort a function and an entire operation (call stack) if/when something in the call environment (parameters, object state, global state) makes the function's operation impossible or invalid. Passing a zero param to a function that needs to divide a quantity by that param, for example. Division by zero won't produce a meaningful result, and if that's the sole purpose of the function, then the function can't return a meaningful result either. So, throw an exception. This will cause execution to jump to the nearest catch or finally block on the call stack. There is no returning to the function that threw the exception.

If you want to step into your code in the debugger to trace the Console.WriteLine() calls, you need to remove the throw new NotSupportedException() line from your code and recompile.

like image 41
dthorpe Avatar answered Oct 07 '22 14:10

dthorpe