40

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

3
  • 1
    based on the above code if the exception is thrown the appropriate catch block will intercept it. Any statement coming after the catch block will get executed!!! Commented Jun 9, 2010 at 16:05
  • 3
    It sounds like you want C#'s version of BASIC's ON ERROR RESUME NEXT. Commented Jun 9, 2010 at 16:16
  • Eh... the program doesn't hit the Console.WriteLine(@class), nor the previous line becuase the line... throw new NotSupportedException() , does literally that. It throws an exception and execution stops. Commented Aug 22, 2022 at 19:54

11 Answers 11

44

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.

Sign up to request clarification or add additional context in comments.

Comments

14

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.

Comments

9

If you're worried that an exception will be thrown in the method but you want the method to continue, add an error handler inside the method.

class Test { public void s() { try { // Code that may throw an exception throw new NotSupportedException(); } catch(Exception ex) { // Handle the exception - log?, reset some values? } string @class = "" ; Console.WriteLine(@class); Console.ReadLine(); } } 

You could also return a bool or some other value to indicate the state.

Comments

3

Disclaimer: I am not suggesting that you actually do this.

You can mimic the old VB style On Error Resume Next with the following code.

public static class ControlFlow { public static Exception ResumeOnError(Action action) { try { action(); return null; } catch (Exception caught) { return caught; } } } 

And then it could be used like the following.

public static void Main() { ControlFlow.ResumeOnError(() => { throw new NotSupportedException(); }); ControlFlow.ResumeOnError(() => { Console.WriteLine(); }); ControlFlow.ResumeOnError(() => { Console.ReadLine(); }); } 

Comments

2

Some simple code I put together to catch exceptions that are thrown inside a catch block:

try { //do code here } catch (Exception ex) { try { SomeMethod1(); } catch { } try { SomeMethod2(); } catch { } try { SomeMethod3(); } catch { } } finally { //cleanup goes here } 

1 Comment

finally block executes whether an exception is thrown or not thrown
0

Execution is still carying on but there is no code after the exception is caught. If you want to repeatedly call s then consider wrapping the try/catch block in a while loop.

Comments

0

The program stops running because there is no following code to be executed in the Main() method! You can add the following line to your code to keep the program running until there is a console input:

Console.ReadLine(); 

Comments

0

For that code, you can't. If you break the tasks up to smaller chunks, you can resume at the next chunk. But normally it's easier to have a different mechanism than exceptions to report non-fatal errors, such as a callback function which returns whether or not to continue.

Comments

0

You can use the "step-over" feature in debugging to achieve this on a per-run basis.

Comments

0

Instead of thowing the NotSupportedException, you could track that an exception was encountered, use a default value, and throw the exception at the end of the method call:

namespace ConsoleApplication1 { public class Test { public void s() { bool exceptionEncountered = false; if(someConditionNotSupported){//stub condition exceptionEncountered=true @class="DefaultValue"; } Console.WriteLine(@class); Console.ReadLine(); if(exceptionEncountered){ throw new NotSupportedException(); } } } public class Program { public static void Main(string[] args) { try { new Test().s(); } catch (ArgumentException x) { } catch (Exception ex) { } } } } 

Comments

-7
public static void Main() { for (int j = 0; j <= 100000; j++) { try { // TODO: Application logic... } catch { System.Threading.Thread.Sleep(1000); } } } 

4 Comments

It will just wait a second and then try executing again, provided the error was a temporary one
Except your "application logic" will get repeated 100000 times no matter what.
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
if you changed the line System.Threading.Thread.Sleep(1000); to simply Main() you might be onto something

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.