views:

237

answers:

3

I tried two ways of catching unexpected unhandled exceptions:

static void Main()
        {
            AppDomain.CurrentDomain.UnhandledException += 
                new UnhandledExceptionEventHandler(ErrorHandler.HandleException);

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            try
            {
                Application.Run(new OCR());
            }
            catch (Exception ex)
            {
                ErrorWindow errorWindow = new ErrorWindow(ex);
                errorWindow.ShowDialog();
                Application.Exit();
            }
        }

When i execute the application using visual studio, everything works fine. if i use exe file in the bin\Debug folder, the exceptions are not handled. Application behaves as if catch block wasn't there. I'm clueless what's going on. any ideas?

edit: exceptio nis not thron in Load

+4  A: 

I take it that you have an exception in your Form's OnLoad method or Load event, then. This is a notorious issue, which isn't helped by the IDE making it behave differently. Basically, you need to make sure that your OnLoad/Load don't throw... perhaps put the catch in there, and set a property you can check afterwards?

Marc Gravell
well that's true! i did put it in load to see what happens. but before that i had an exception thrown in a different part of code, long after load. i didn't have the try catch block then but a had the UnhandledException handler and it wasn't fired as well.
agnieszka
A: 

I've run into an interesting issue when dealing with J# code.

Assume that the exception being thrown is of type FreakyException. Now, FreakyException inherits from FreakyExceptionBase class, which in turn inherits from Exception class. The trick is that each of them are defined in their own assemblies. Say, FreakyExceptionBase resides in ExceptionBases.dll, but FreakyException itself resides in Worker.dll. Your application only references the Worker.dll, but NOT the ExceptionBases.dll.

In this case your catch(Exception) won't catch FreakyException because .NET will not be able to figure out the inheritance chain that specifies that FreakyException actually inherits form Exception.

This happened with J# code because J# exceptions inherit from java.lang.exception, and my project wasn't referencing J# libraries. When J# was referenced, the problem went away.

Perhaps it is the same case with your application?

Vilx-
not really because i was throwing ArgumentException and it inherits from system.exception from sure. it's good to know what you've written though.
agnieszka
+2  A: 

I should handle System.Windows.Forms.Application.ThreadException - then it works.

agnieszka
still don't know what's wrong with try catch block but at least the handler works fine.
agnieszka
Perhaps the exception got thrown in another thread?
Vilx-
Or there was another exception handler along the way that already caught it.
Vilx-
I was going to answer your question by suggesting to handle that event. It does not have to be in the Load event, it may be anywhere in a code executed in a thread other than the main thread.
Recep