开发者

General exception handling fails for async calls

I'm implementing something similar to Handling unhandled exceptions problem :

        Application.ThreadException +=
            (s, eargs) => LogException(eargs.Exception);
        AppDomain.CurrentDomain.UnhandledException +=
            (s, eargs) =>
                LogException((Exception)eargs.ExceptionObject);
   开发者_运维问答     Application.SetUnhandledExceptionMode(
            UnhandledExceptionMode.CatchException);

This has been working well in all of my applications until now. When I encounter an error in this asynchronous handler:

    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);
    }

I am able to catch it if I add

        AppDomain.CurrentDomain.FirstChanceException +=
            (sender, eargs) => LogException(eargs.Exception);

but not otherwise. I don't want to have to use FirstChanceException because that seems to be nasty and defeat the whole purpose of exception handling.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜