Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UnhandledException Event doesn't work?

I'm writing a little library which catches all unhandled exceptions, shows a little dialog (similar to the usual dialog of the NF) which gives the user the opportunity to send the exception to the developer. To do this, I use the UnhandledException-Event of the AppDomain like this:

app.UnhandledException += (object sender, UnhandledExceptionEventArgs e) =>
        {
            ExceptionHandler handler = new ExceptionHandler((Exception)e.ExceptionObject, ExEntry);
            UnhandledExceptionListened(handler);
            if (Properties.Settings.Default.ShowStandardExceptionDialog)
            {
                ExceptionDialog exdialog = new ExceptionDialog(handler);
                exdialog.ShowDialog();
            }
        };

ExceptionHandler and ExEntry are Classes of my Library. But: If an Exception occurs, the compiler jumps into my Lambda-Expression, tries to debug the first line of code and then shows the error which occurred before without working off the rest of the lambda. But if I just write:

 app.UnhandledException += (object sender, UnhandledExceptionEventArgs e) =>
        {
                ExceptionDialog exdialog = new ExceptionDialog(handler);
                exdialog.ShowDialog();
        };

it works perfectly. Has anyone an idea why this doesn't work?

like image 898
Reignbeaux Avatar asked May 23 '13 15:05

Reignbeaux


People also ask

Which event is used for unhandled exception?

The UnhandledException event is raised for unhandled exceptions thrown in other threads. Starting with Microsoft Visual Studio 2005, the Visual Basic application framework provides another event for unhandled exceptions in the main application thread. See the WindowsFormsApplicationBase. UnhandledException event.

What is unhandled exception?

An unhandled exception is an error in a computer program or application when the code has no appropriate handling exceptions. Learn about the definition and examples of unhandled exceptions, and explore programming and exception handlers. Updated: 01/04/2022.

What is unhandled exception in C#?

An unhandled exception occurs when the application code does not properly handle exceptions. For example, When you try to open a file on disk, it is a common problem for the file to not exist. The . NET Framework will then throw a FileNotFoundException.


1 Answers

There might be two reasons.

One is you did not set UnhandledExceptionMode properly:

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

The other is you did not handle the ThreadException, and the thrown exception was not an unhandled exception but a thread exception.

The following is an example, you would need to modify it according to your scenario:

Application.ThreadException+=
    new ThreadExceptionEventHandler(Log.WriteThreadException);

AppDomain.CurrentDomain.UnhandledException+=
    new UnhandledExceptionEventHandler(Log.WriteUnhandledException);

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
like image 186
Ken Kin Avatar answered Oct 06 '22 01:10

Ken Kin