Very strange exception handling problem

G

Guest

Hi,

I have a .NET solution that contains a dll project and a tester application
project which, of course, invokes the dll. The dll project has exception
handling in it. What's happening is that when I run the executable from
within the .NET studio environment, thrown exceptions are caught and handled
correctly (both debug and release mode). However, if I run the executable
from outside the .NET studio, none of the exceptions get caught. In fact,
what I get is a pop-up telling me an unhandled exception has occurred and
pointing me right back to where the (valid) exception was thrown from. I
thought perhaps it was something I was doing wrong in my solution so I
created a little bare-bones dll and test app but the same thing still happens.

Has anyone ever seen anything like this? Any ideas as to what the problem
might be would be greatly appreciated! Btw, I had another much more
experienced co-worker look at this as well and he couldn't figure it out
either.

Regards,
Robin
 
G

Guest

Robin,
Can you post a "Short but Complete" code sample of exactly how you are
catching and handling an exception?
Peter
 
G

Guest

Hi Peter,

Thanks for the quick response! Here's a simple example:

internal void doStuff()
{
try
{
doMoreStuff();
}
catch(System.Exception x)
{
MessageBox.Show(x.Message + x.Trace);
}
}

private void doMoreStuff()
{
// assume error condition occurs herehere
throw new Exception();
}
 
G

Guest

BTW, I did some further playing around and noticed that this failure to catch
exceptions only seems to occur when the exception is thrown from within a
windows form.

Regards,
Robin
 
B

Bruce Wood

I'm guessing from your description that this is a WinForms application.

When you run a WinForms application under control of the Visual Studio
debugger (regardless of whether it's compiled for release or debug
mode), execution flow is exactly the way the code reads: just what you
would expect.

However, when you run it directly under Windows, execution flow
changes. Someone explained the precise details to me once but my memory
is a bit foggy on details. I'll give the best explanation I can,
though.

If you invoke a dialog, something like this:

DialogResult result = myDialog.ShowDialog();

you would think that you could catch any exceptions raised by the
dialog like this:

try
{
DialogResult result = myDialog.ShowDialog();
...
}
catch (Exception e)
{
...
}

and any exception raised by the dialog would be caught in the "catch"
block. In Visual Studio, this is exactly what happens.

However, when you run directly under Windows, the system doesn't really
call your dialog and then just sit there inside the try block waiting
for it to return. Somehow (I'm not sure how), control returns to the
underlying windowing system, which shows the dialog. Control is often
returned to the underlying system at strategic points in your
application. That's why when that dialog throws an exception, the
exception bubbles up not to your calling code, but right back to the
windowing system and the CLR, where it is treated like an unhandled
exception.

As I said, my memory of this isn't perfect, and my example may not be
exactly right, but that's basically what's going on in WinForms.

The only way to catch these exceptions and log them is by handling the

AppDomain.UnhandledException

and

Application.ThreadException

events.
 
B

Brian

Robin said:
BTW, I did some further playing around and noticed that this failure to catch
exceptions only seems to occur when the exception is thrown from within a
windows form.

Regards,
Robin

Ahh yes, windows forms exceptions.

You need to attach an exception handler to the thread's ThreadException
event:

Code:
System.Windows.Forms.Application.ThreadException +=
OnThreadException;

And have a code handler:

Code:
/// <summary>
/// Provides a last chance exception handler.
/// </summary>
public static void OnThreadException(object sender,
System.Threading.ThreadExceptionEventArgs e)
{
log.Error("Unhandled Exception Occured", e.Exception);
}
 
G

Guest

Thanks to those who replied - I can now stop banging my head against the
wall!! :)

Regards,
Robin
 

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