例如:
第一步:绑定UnhandledException事件。
第一步:绑定UnhandledException事件。
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
第二步:
try
{
//at here,do asynchronous operation and throw a exception.
}
catch (Exception ex)
{
Console.WriteLine("error");
}
当异步操作抛出异常时,捕获代码不会被调用,只有UnhandledException事件被触发,在结束事件被调用后才退出应用程序。
我希望在catch语句中捕获任何异常并避免退出应用程序。
=======================================================
异步代码是异步套接字操作。在套接字异步接收消息事件(BeginReceive、EndReceive)中,我抛出了一个OverflowException。
throw new OverflowException("chunk size too long.");
=============================================
你说得对,现在我在异步操作中捕获异常并将其传递给原始类(这意味着异常将在同一线程上抛出,可以尝试...catch语句将被调用)。
async
/await
方式)甚至可能会吞噬异常。 - Vlad