开发者

Exception caught when project debugged from IDE but not when run outside of IDE

In C#, WinForms, VS2008, .NET 3.5...

For this code:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {

        try
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new FormThatDividesByZero());
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }
}

public partial class FormThatDividesByZero : Form
{
    public FormThatDividesByZero()
    {
        InitializeComponent();
    }

    private void DivideByZeroButton_Click(object sender, EventArgs e)
    {
        // Create a divide by zero exception.
        int a = 0;
        int b = 0;
        int c = a / b;
    }
}
开发者_高级运维

Full source: http://forgefx.com/posts/ExceptionReporting.zip

When I run this small test project, via F5, from the development environment, the exception after clicking the DivideByZero button is caught and the message box triggers. When I run the project by double-clicking the .exe in the /bin/Debug folder, the exception is not caught and there is no message box - why is this the case?

When the .exe is launched from outside of the IDE, or with "Debug > Start Without Debugging" from within visual studio, I get an unhandled exception. My understanding was that the code above would catch all exceptions.


This isn't good way to catch global exception. You can use AppDomain or Application object to catch it. This object catch all unhandled exception and call event.

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {

            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new FormThatDividesByZero());            

    }

    static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        Exception ex = (Exception)e.ExceptionObject;
        MessageDialog.Show(ex.Message);
        Application.Exit();
    }

    void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
    {
        MessageDialog.Show(e.Exception.Message);
    }
}



public partial class FormThatDividesByZero : Form
{
    public FormThatDividesByZero()
    {
        InitializeComponent();
    }

    private void DivideByZeroButton_Click(object sender, EventArgs e)
    {
        // Create a divide by zero exception.
        int a = 0;
        int b = 0;
        int c = a / b;
    }
}


Try adding an event handler for the Application.ThreadException event.

Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
    MessageBox.Show("ThreadException:"+e.Exception.Message);
}

In this way you can deal with exception uncaught in any event code without ending the application, for example by logging or alerting. Of course whether the application is still stable after such an unexpected exception needs to be considered, i.e. is it meaningful for the app to continue running?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜