for example:
first step:bind UnhandledException event.
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
second step:
try
{
//at here,do asynchronous operation and throw a exception.
}
catch (Exception ex)
{
Console.WriteLine("error");
}
when the asynchronous operation throw a exception,the catch code it not called,just only the UnhandledException event is triggered,after end event called then exit application.
i want capture exception anything in catch statement and avoid exit application.
=======================================================
the asynchronous code is the asynchronous socket operation.in the socket asynchronous receive message event(BeginReceive,EndReceive),i throw a OverFlowException.
throw new OverflowException("chunk size too long.");
=============================================
you are right,now i capture the exception in asynchronous operation and passed it to original class(this means exception will throw on the same thread,that can try…catcy statement can was called)
An asynchronous task by default will operate in a different context from the one it was instantiated in. The try/catch block is therefore not effective in this case.
Think about it in the following way.
Here we see that when the worker gets sick, the work process breaks and the exception will be handeled. However, when we do:
The work try/catch block (or safety net if you will), will only apply for the worker, not otherWorker. The otherWorker has its own scope to work in. If the other worker fails, that shouldnt mean that the worker has to take the medicine.