Exception when trying to debug a project from the IDE, but not when starting outside the 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 start this small test project, through F5, from the development environment, the exception after clicking the DivideByZero button breaks and the message box starts. When I start the project by double-clicking the .exe file in the / bin / Debug folder, the exception is not caught and there is no message box - why is this?

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

+3
2

. AppDomain Application, . .

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;
    }
}
+1

Application.ThreadException.

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

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

>

, , , , , . , , , ?

0

Source: https://habr.com/ru/post/1752400/


All Articles