0

Possible Duplicate:
How do I continue running after an unhandled exception?

I've seen tons of similar questions and anwsers on SO, but not the same.

I'm handling all uncaught exception in main like this:

In Main I set following:

Application.ThreadException += new ThreadExceptionEventHandler(MainForm_UIThreadException);

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

AppDomain.CurrentDomain.UnhandledException +=
                 new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

Application.Run(new MainForm());

If I run my programm in debugger, after the exception occured and I handled them in MainForm_UIThreadException or CurrentDomain_UnhandledException, the yellow debugger arrow points to the line, where this exception actually occured, so I can step over, which means (for me) that execution context isn't lost.

I know that I cant continue execution for unhandled exceptions, but this esception is actually handled, just somewhere else.

Is it possible to continue execution of current function?

Thank you

Community
  • 1
  • 1
VladL
  • 12,769
  • 10
  • 63
  • 83
  • 2
    Do you really think it is a good idea to continue after an unhandled exception occurred? Your application is in an unknown state and data may be corrupted. – Oded Dec 12 '12 at 13:06
  • In general I agree with you, it's not a good idea, but sometimes it is useful. But I need to know if it's possible or not – VladL Dec 12 '12 at 13:13
  • not a better dupe as there is nothing about 'ThreadException' – VladL Dec 12 '12 at 13:56

2 Answers2

1

You don't. When you get an AppDomain unhandled exception, your app is no longer in a stable state. Where exactly would you resume to? When you've gotten to that point, your only option, with good reason, is to exit. You could reasonably schedule yourself to run again to make sure the app comes back, but a far better idea is to actually handle the exception at its source and prevent it from being an UnhandledException.

From: How do I continue running after an unhandled exception?

Community
  • 1
  • 1
jAC
  • 5,195
  • 6
  • 40
  • 55
  • 1
    instead of duplicating the answer, you can mark this question as a duplicate to that question. – default Dec 12 '12 at 13:25
  • it's a different story, that was the question about windows mobile and he couldn't even prevent closing of his app. In my case just the function, which caused an exception is interrupted... at least in case of UI exceptions – VladL Dec 12 '12 at 13:28
  • @Default it's not duplicate, different platforms... – VladL Dec 12 '12 at 13:28
  • Still it's C#, so it won't behave differently. @Default: I will do that next time. Thanks for the advice, didn't know that. – jAC Dec 12 '12 at 13:36
  • It does behave differently if we take a look on his and on my case. And still nothing about `ThreadException´ – VladL Dec 12 '12 at 14:12
  • You may use try-catch-finally and put everything in the finally block. It gets executed then, if it throws an exception. – jAC Dec 12 '12 at 14:18
  • Thank you. I know the basic concept of exception handling and that is not my question. – VladL Dec 12 '12 at 14:33
0

In a WinForms application, if you want to continue running, you need to handle exceptions in your UI event handlers.

For example:

private void saveButton_Click(object sender, EventArgs e)
{
    try
    {
        ... call BLL to save data
    }
    catch(Exception ex)
    {
        MessageBox.Show("Failed to save data");
    }
}
Joe
  • 122,218
  • 32
  • 205
  • 338
  • it's not an answer on my question, because my exception is **handled** just somewhere else – VladL Dec 12 '12 at 13:57
  • @VladL you still throw the exception. It's not in the same context *when it's handled* – default Dec 12 '12 at 14:18
  • It's correctly handled inside of MainForm_UIThreadException – VladL Dec 12 '12 at 14:19
  • @Default And as I said, debugger arrow points to the position in code there the exception was thrown and I can step over which makes me think it returns to the same context after exception is handled – VladL Dec 12 '12 at 14:46
  • 1
    @VladL, "...debugger arrow points to the position in code there the exception was thrown and I can step over which makes me think it returns to the same context after exception is handled" - that's a misunderstanding. – Joe Dec 12 '12 at 15:17
  • @Default If I can continue running the code, that does mean I'm on the same context. I couldn't continue running it if current context has switched. What do I understand wrong? – VladL Dec 12 '12 at 15:32
  • Joe I think he means you. @VladL unfortunately I do not know the Visual Studio debugger very well or how Visual Studio handles code that is under debug mode. When you throw an exception the call stack is preserved, meaning you know where you came from. Visual Studio also has tools which makes it possible to step over code and what not. That doesn't automatically mean it is in a consistent state when you do it. – default Dec 12 '12 at 15:33