Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

c# - Is there a setting that is preventing the unhandled exception dialog from displaying in apps that I compile?

I'm not sure if this is the right place to ask but I shall anyway.

I have 2 .NET applications; one that was compiled by myself, the other not. Both use .NET Framework 4.5. But they handle exceptions differently.

In the application which I did NOT compile; it shows the unhandled exception dialog (which I want and expect)

Unhandled Exception from app I didnt compile

But in the application I did compile; it just shows the application has crashed;

Unhandled Exepction from app I compiled

So there must be a setting in the VS config or project config that is preventing the unhandled exception dialog from displaying in apps that I compile?...

I have tried re-installing VS, changing the settings in the Debug->Exceptions menu, neither have worked...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The top screen-shot is a ThreadExceptionDialog. It is displayed in the very specific case where a Winforms app bombs in an event handler that was fired by the message loop (Application.Run) and the app didn't otherwise reassign the Application.ThreadException event handler. Using it is not a very good practice, there's no reasonable way the user could know whether to click the Continue or Quit button. Be sure to call Application.SetUnhandledExceptionMode() to disable it.

The bottom screen-shot is the default Windows error reporting dialog, displayed by Windows when a program bombed on an unhandled exception. You should never let it get to this point, the dialog doesn't display enough information to help anybody diagnose and fix the problem. Always write an event handler for the AppDomain.CurrentDomain.UnhandledException event. Display and/or log e.ExceptionObject.ToString() and call Environment.Exit() to terminate the app.

Make your Program.cs source code look similar to this:

    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
        AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
        Application.Run(new Form1());
    }

    static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {
        // TODO: improve logging and reporting
        MessageBox.Show(e.ExceptionObject.ToString());
        Environment.Exit(-1);
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...