exception bubbling

J

jay.meerdink

I have a simple app that opens in Program.cs with

try
{
Application.Run(new Shell());
}
catch (Exception ex)
{
LogErr(ex)
}

In the development environment, any exception thrown anywhere in any
form fires the catch in Program.cs. Create a button in Shell that
opens a new form Test. Test has one button that throws a new
exception. When running in the design environment, the exception
rises to the catch in Program.cs. If I run the executable, the throw
results in an unhandled exception error box.

Why would this behavior be different from one environment to the
other? How do I get the executable to react to errors in the same
manner as the development environment?

Thank you!
J
 
B

Bruce Wood

I have a simple app that opens in Program.cs with

try
{
Application.Run(new Shell());
}
catch (Exception ex)
{
LogErr(ex)
}

In the development environment, any exception thrown anywhere in any
form fires the catch in Program.cs. Create a button in Shell that
opens a new form Test. Test has one button that throws a new
exception. When running in the design environment, the exception
rises to the catch in Program.cs. If I run the executable, the throw
results in an unhandled exception error box.

Why would this behavior be different from one environment to the
other? How do I get the executable to react to errors in the same
manner as the development environment?

Thank you!
J

Look at the stack dump for the exception when you're not running under
the debugger: your button's handler isn't being called by your
application, is it? It's being called by Windows.

This has to do with how the Windows message pump works. The operating
system subverts the apparent control flow in your program so that it
has time in between to do things like redraw windows, minimize,
resize, and move things around.

The apparent control flow is maintained in the debugger to help you
find errors, but it's not the real way that your program works.

Instead of the catch-all that you have around your Application.Run
call, you should use the two global exception events:
AppDomain.UnhandledException and Application.ThreadException. Search
for these in this newsgroup, or on MSDN, and you'll find documentation
about how to add global exception handlers to your program.
 
K

Kevin Spencer

I just happen to have an app that does this. Here's some sample code:

AppDomain.CurrentDomain.UnhandledException += // CLR
new UnhandledExceptionEventHandler(OnUnhandledException);

Application.ThreadException += // Windows Forms
new System.Threading.ThreadExceptionEventHandler(
OnGuiUnhandedException);


// CLR unhandled exception
private static void OnUnhandledException(Object sender,
UnhandledExceptionEventArgs e)
{
HandleUnhandledException(e.ExceptionObject);
}

// Windows Forms unhandled exception
private static void OnGuiUnhandedException(Object sender,
System.Threading.ThreadExceptionEventArgs e)
{
HandleUnhandledException(e.Exception);
}

static void HandleUnhandledException(Object o)
{
Exception e = o as Exception;

if (e != null)
{ // Report System.Exception info
//Debug.WriteLine("Exception = " + e.GetType());
//Debug.WriteLine("Message = " + e.Message);
//Debug.WriteLine("FullText = " + e.ToString());
Utilities.HandleError(e);
}
else
{ // Report exception Object info
//Debug.WriteLine("Exception = " + o.GetType());
//Debug.WriteLine("FullText = " + o.ToString());
Utilities.LogError("Exception: " + o.GetType().ToString() +
"\r\nFullText: " + o.ToString());
}

MessageBox.Show("An unhandled exception occurred " +
"and the application is shutting down.");
Environment.Exit(1); // Shutting down
}

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

The shortest distance between 2 points is a curve.
 
J

jay.meerdink

I just happen to have an app that does this. Here's some sample code:

AppDomain.CurrentDomain.UnhandledException += // CLR
new UnhandledExceptionEventHandler(OnUnhandledException);

Application.ThreadException += // Windows Forms
new System.Threading.ThreadExceptionEventHandler(
OnGuiUnhandedException);

// CLR unhandled exception
private static void OnUnhandledException(Object sender,
UnhandledExceptionEventArgs e)
{
HandleUnhandledException(e.ExceptionObject);

}

// Windows Forms unhandled exception
private static void OnGuiUnhandedException(Object sender,
System.Threading.ThreadExceptionEventArgs e)
{
HandleUnhandledException(e.Exception);

}

static void HandleUnhandledException(Object o)
{
Exception e = o as Exception;

if (e != null)
{ // Report System.Exception info
//Debug.WriteLine("Exception = " + e.GetType());
//Debug.WriteLine("Message = " + e.Message);
//Debug.WriteLine("FullText = " + e.ToString());
Utilities.HandleError(e);
}
else
{ // Report exception Object info
//Debug.WriteLine("Exception = " + o.GetType());
//Debug.WriteLine("FullText = " + o.ToString());
Utilities.LogError("Exception: " + o.GetType().ToString() +
"\r\nFullText: " + o.ToString());
}

MessageBox.Show("An unhandled exception occurred " +
"and the application is shutting down.");
Environment.Exit(1); // Shutting down

}

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composerhttp://unclechutney.blogspot.com

The shortest distance between 2 points is a curve.










- Show quoted text -

Thanks all!
 

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