Visual Studio 会中断异步调用的异常,而不是被 catch() 捕获

本文关键字:catch 捕获 中断 Studio 异步 调用 异常 Visual | 更新日期: 2023-09-27 18:33:24

我有以下代码试图捕获由断开连接引起的异常:

try
{
    DataReaderLoadOperation op = dataReader.LoadAsync((uint)readBuffer.MaxLength());
    await op;  //  <-- Debugger show COMException here
    ReceiveCallback(op, op.Status);
}
catch (COMException e)
{
    Logger.Info("{0} disconnected. (Waiting)", this);
    Logger.Debug("{0} exception = {1}", this, e);
    OnDisconnected();
}

为什么我的代码没有捕捉到它?以下是异常的转储:

System.Runtime.InteropServices.COMException occurred   
  HResult=-2147014842   
  Message=An existing connection was forcibly closed by the remote host. (Exception from HRESULT: 0x80072746)   
  Source=mscorlib   
  ErrorCode=-2147014842   
  StackTrace:
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
       at Crystal.IO.Network.SocketClient.<WaitForIncoming>d__4.MoveNext() in d:'Crystal'library'IO'Network'SocketClient.cs:line 479   
  InnerException:

Visual Studio 会中断异步调用的异常,而不是被 catch() 捕获

更改调试器设置,以便在第一次机会异常时不中断。