views:

243

answers:

5

Hi: I have many asynchronous operations in different class.

When error,it will throw special exception which is inherited from System.Exception.
In some form,I wanna catch it by messageBox.

The function "Application.ThreadException" cannot catch it.

In other word,I cannot catch all the exceptions by the function when there is some exception thrown How can I do?

+2  A: 

Basically you should handle exceptions on a per-thread basis, with Application.ThreadException as a last resort.

How to do that depends on what Async pattern you are using, but for instance when using IAsyncResult the exception is 'stored' for you and thrown when you call EndIvoke(.., iar)

Henk Holterman
eg,we can catch exception and redirect page to the errorpage in asp.net by setting the error data in web.config.how to do in winform?
Edwin Tai
A: 

i can catch all the exception when it throws in any thread
but it is not a good idea.
Is there any solution to catch exception in the main thread.

Edwin Tai
+3  A: 

Subscribe to Application.UnhandledException it fires on any unhandled exception and exposes the exception object with details in EventArgs.

Ray
A: 

As Henk Holterman has already suggested you should handle exceptions. Only those nasty ones which are unhandled should be left for Application.ThreadException. In case you are not aware then, although you will be able to display your message in the Application.ThreadException event handler method, the application will then terminate. Certain exceptions are not caught by Application.ThreadException and you will have to handle AppDomain.UnhandledException Event. AFAIK It is best not to throw exceptions in Async handlers (gurus, correct me if I am wrong). The best way to deal with them would be to either throw exceptions when user calls end invoke or when the user tries to retrieve response objects. Please note if you throw exc object from one thread it won't be caught in some other thread. So, to inform the main thread one way would be to raise an event (which the main thread has hooked onto) in which you pass the response object. If an error has occured then raise an exception in the getter of the response object else return the response object. Another way would be to just raise an event which would tells the main thread just about the exception.

EDIT: Just saw your comment:

we can catch exception and redirect page to the errorpage in asp.net by setting the error data in web.config. how to do in winform?

There isn't a direct way to do it in winforms. The way I do it is have one HandleError method per thread. This method will accept exception object as parameter and inspect the type of exception will display a message box and/or log it.

P.K
A: 

Exceptions are bad in ASync applications because exceptions can only be handled one at a time across all threads (from MSDN). Currently you can handle it with Events/Delegates.

VS2010 has a new exception handler for handling multi-threaded exceptions.

IPX Ares