9

Code in Program.cs

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    try
    {
        Application.Run(new Form1());
    }
    catch (Exception ex)
    {
        MessageBox.Show("Blah...");
    }
}

In Form1 I have a button with the code throw new Exception("");.

If I run the application from Visual Studio, then my messagebox pops up (with message 'Blah...'). But if I run the application from executable file, then the catch block doesn't execute at all.

Why the difference?

I am using Visual Studio 2010, .NET 4.0, Windows XP.

sventevit
  • 4,766
  • 10
  • 57
  • 89
  • What does happen when running outside VS? What happens if you press Ctrl+F5? (Run without debugging) – SLaks Jul 18 '11 at 14:19
  • 2
    Short, incomplete answer: Stuff happens differently while debugging. See this question for what you *should* be doing: http://stackoverflow.com/questions/1601372/is-try-catch-around-whole-c-program-possible –  Jul 18 '11 at 14:20

3 Answers3

17

This is because the standard exception handling for a Windows Forms application behaves differently when the Visual Studio debugger is attached - normally the exception handler built into the Application.Run method catches unhandled exceptions so that it can do things like show the following dialog:

Error dialog

If it allowed the exception to be thrown outside of the Application.Run method then it would prevent the application from continuing if the user presses "continue" (as the catch is outside of the message pump).

When debugging however this is disabled, presumably so that the debugger will jump straight into debugging mode on an unhandled exception rather than the above dialog being shown.

If you wish to handle unhandled exceptions in your Windows Forms application then you should handle the Application.ThreadException Event. Alternatively you can alter this behaviour with the Application.SetUnhandledExceptionMode Method.

You are by no means alone in being confused by this:

Community
  • 1
  • 1
Justin
  • 84,773
  • 49
  • 224
  • 367
-1

Because in RELEASE mode at the moment the message box have to be shown yuo're appication is going to be "dead" and you can not stop it by showing MessageBox. In DEBUG mode VS cares about that and breaks on the line that throws an exception.

Regards.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • @Slaks: What is the mistake here ? – Tigran Jul 18 '11 at 14:20
  • 1
    Everything. No part of your answer is true. – SLaks Jul 18 '11 at 14:21
  • Sorry guys, but the question here is not WHAT he should doing, but why the message box doesn't show in RELEASE by having THIS code. If you have complete accurate answer to this, I will be only happy to read it. Regards. – Tigran Jul 18 '11 at 14:25
  • @Slaks: with all respect, but explain at least why. Or Add an answer so people can understand. – Tigran Jul 18 '11 at 14:27
  • 2
    1) He isn't running Release mode. 2) `catch` blocks work fine in release mode 3) VS isn't breaking in debug mode; he's seeing the MessageBox 4) The actual problem is the default unhandled exception handler, which doesn't run when the debugger is attached. – SLaks Jul 18 '11 at 14:29
  • @Slaks: by saying RELEASE I mean running RELEASE binaries. Ok I will update my post. – Tigran Jul 18 '11 at 14:33
  • 1
    I know. He **isn't** running Release binaries. That doesn't make your answer any less wrong. – SLaks Jul 18 '11 at 14:36
  • Ok, I got it. But, honestly it's impossible, and potentially possible only in case if it run RELEASE, at least makes some difference :) Regards. – Tigran Jul 18 '11 at 14:38
  • Also - it doesn't matter if it is compiled in debug or release mode, the problem remains the same. – sventevit Jul 18 '11 at 18:15
  • @sven: it clearly does, it's enough to try it in the code. cause the question is about message box. try it,and only after downvote. – Tigran Jul 18 '11 at 18:21
-1

I can't see why this is happening but try rebuilding the solution, if in doubt I always do that. It may or may not fix the problem and you probably have already tried but if you haven't it may fix it for you.

JakeJ
  • 1,381
  • 3
  • 14
  • 34