views:

135

answers:

2

Hi, In my multithread server I am using following code (before running Form itself or course)

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomainUnhandledException);
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(ApplicationThreadException);

The called methods write exceptions to the list so I can fix it easily. But today I came and saw that VS has thrown NullReference exception and I have no clue why it was not catched?

I can reproduce it by calling: stream=null; stream.Flush()

The program do jump to exception catching scope but it stays on the Flush line..strange. Any other exception is handled well..maybe its beacuse this method is not implemented yet?

Im sure I have seen "Object reference not set to an instance of object" in my "exceptions" list and the program continued. Why isn't this particular exception caught?

A: 

To answer your question, we need more information about the error that was thrown. Does it originate from your code? Let's see the stack trace.

Also, if you call Application.Run(...) before setting up the ThreadException, it won't catch any exceptions.

Judah Himango
? I think you wrote it vice versa, you need to set threadexception before you run the app...Anyway, it works fine, still catch the expcetion, but the only one after weeks was not catched - I called future function stream.FLush() and the stream was null.
Tomas
My mistake, that's what I meant to say.
Judah Himango
Huf, I was affraid I didnt understand it well. I tried it again with setting stream to null and then call Flush - with debugger, the exception is catched but it stays stucked on the line Flush and still repeats..maybe because its not implemented?
Tomas
What do you mean "stays stucked on the line"? Are you under the debugger? Please know that the debugger intercepts unhandled exceptions before hitting things like ThreadException handler.
Judah Himango
A: 

Hi there,

We use the following code and find it works well: -

namespace YourNamespace
{
    static class Program
    {

        [STAThread]
        static void Main()
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            HandleException(e.Exception);
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            HandleException((Exception)e.ExceptionObject);
        }

        static void HandleException(Exception e)
        {
            //Handle exception
        }

    }
}

Thanks,

Phil http://exceptioneer.com

Plip