Global exception handling to show a form


N

n33470

Hi all,

Is there a way to display a modal dialog in the
AppDomain.CurrentDomain.UnhandledException delegate?

I'd like to display a custom dialog, in addition to logging the error,
on any globally unhandled exceptions. I have registered for the
UnhandledException, but while that method runs the modal dialog shows,
but the app keeps shutting down and terminates. Is there a known
limitation that restricts showing forms during the global exception
handler?

Here is a small sample that gives you the basic idea. I put a button
click event on the Login screen that deliberately throws an
ApplicationException. The MessageBox in the delegate shows for a
moment, but then the app terminates. I'm running on the emulator and
this behavior happens both with and without the debugger.

[MTAThread]
static void Main()
{
AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(OnUnhandledException);
Application.Run(new Login());

}

public static void OnUnhandledException(Object sender,
UnhandledExceptionEventArgs e)
{
Exception ex = e.ExceptionObject as Exception;
if (ex != null)
{
this.LogError(ex);
MessageBox.Show(ex.Message, "Unhandled Exception");
}
}

Thanks!

--steve
 
Ad

Advertisements

D

Daniel Moth

You should check the IsTerminating property and, if it is true, do not
expect to hold the process up by a dialog/msgbox. I didn't pursue your goal
but recall observing the same results with the november ctp.
http://www.danielmoth.com/Blog/2004/12/appdomainunhandledexception-part-1.html

As a workaround consider logging to a file the details, and launching
another smaller app that reads the details, showing them to the user and
doing whatever else you wish...

Cheers
Daniel
 
S

Shloma Baum

Hi Daniel,

So basicly as you say there is no way of keeping the app of shutting down
when getting an unhandled exception?. If this is true then this is really
weird.

Best Regards,
Shloma Baum

Daniel Moth said:
You should check the IsTerminating property and, if it is true, do not
expect to hold the process up by a dialog/msgbox. I didn't pursue your
goal but recall observing the same results with the november ctp.
http://www.danielmoth.com/Blog/2004/12/appdomainunhandledexception-part-1.html

As a workaround consider logging to a file the details, and launching
another smaller app that reads the details, showing them to the user and
doing whatever else you wish...

Cheers
Daniel
--
http://www.danielmoth.com/Blog/

n33470 said:
Hi all,

Is there a way to display a modal dialog in the
AppDomain.CurrentDomain.UnhandledException delegate?

I'd like to display a custom dialog, in addition to logging the error,
on any globally unhandled exceptions. I have registered for the
UnhandledException, but while that method runs the modal dialog shows,
but the app keeps shutting down and terminates. Is there a known
limitation that restricts showing forms during the global exception
handler?

Here is a small sample that gives you the basic idea. I put a button
click event on the Login screen that deliberately throws an
ApplicationException. The MessageBox in the delegate shows for a
moment, but then the app terminates. I'm running on the emulator and
this behavior happens both with and without the debugger.

[MTAThread]
static void Main()
{
AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(OnUnhandledException);
Application.Run(new Login());

}

public static void OnUnhandledException(Object sender,
UnhandledExceptionEventArgs e)
{
Exception ex = e.ExceptionObject as Exception;
if (ex != null)
{
this.LogError(ex);
MessageBox.Show(ex.Message, "Unhandled Exception");
}
}

Thanks!

--steve
 
N

n33470

Daniel,

Thanks for the information! As a matter of fact, I read your blog
before I started looking into this problem.

It sounds like we'll need to add explicit exception handlers around all
code that may throw an exception, otherwise the process will be aborted
and the app will exit.

--steve
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top