我正在实施与Handling unhandled exceptions problem类似的内容:
Application.ThreadException +=
(s, eargs) => LogException(eargs.Exception);
AppDomain.CurrentDomain.UnhandledException +=
(s, eargs) =>
LogException((Exception)eargs.ExceptionObject);
Application.SetUnhandledExceptionMode(
UnhandledExceptionMode.CatchException);
到目前为止,这在我的所有应用程序中都运行良好。当我在这个异步处理程序中遇到错误时:
void DnsResolveCallback(IAsyncResult result)
{
try
{
IPHostEntry entry = Dns.EndGetHostEntry(result);
IPEndPoint endpoint = new IPEndPoint(entry.AddressList[0], Port);
socket = new Socket(endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
socket.BeginConnect(endpoint, ConnectCallback, null);
}
catch (Exception)
{
SetStatus(ConnectStatus.Disconnected);
throw;
}
SetStatus(ConnectStatus.Connecting);
}
如果我添加
,我就能抓住它 AppDomain.CurrentDomain.FirstChanceException +=
(sender, eargs) => LogException(eargs.Exception);
但不是。我不想使用FirstChanceException
因为这似乎很讨厌并且破坏了异常处理的整个目的。