Development Tip

디버거가 비동기 메서드에서 예외를 중단 / 중지하지 않음

yourdevel 2020. 12. 31. 23:03
반응형

디버거가 비동기 메서드에서 예외를 중단 / 중지하지 않음


디버거가 .NET 프로세스에 연결되면 처리되지 않은 예외가 발생하면 일반적으로 중지됩니다.

그러나 이것은 async메서드 에있을 때는 작동하지 않는 것 같습니다 .

이전에 시도한 시나리오는 다음 코드에 나열되어 있습니다.

class Program
{
    static void Main()
    {
        // Debugger stopps correctly
        Task.Run(() => SyncOp());

        // Debugger doesn't stop
        Task.Run(async () => SyncOp());

        // Debugger doesn't stop
        Task.Run((Func<Task>)AsyncTaskOp);

        // Debugger stops on "Wait()" with "AggregateException"
        Task.Run(() => AsyncTaskOp().Wait());

        // Throws "Exceptions was unhandled by user code" on "await"
        Task.Run(() => AsyncVoidOp());

        Thread.Sleep(2000);
    }

    static void SyncOp()
    {
        throw new Exception("Exception in sync method");
    }

    async static void AsyncVoidOp()
    {
        await AsyncTaskOp();
    }

    async static Task AsyncTaskOp()
    {
        await Task.Delay(300);
        throw new Exception("Exception in async method");
    }
}

내가 뭔가를 놓치고 있습니까? 디버거가 예외에서 중단 / 중지하도록하려면 AsyncTaskOp()어떻게해야합니까?


Debug메뉴 에서을 선택 Exceptions...합니다. 예외 대화 상자에서 Common Language Runtime Exceptions옆에있는 확인란을 선택합니다 Thrown.


이 문제를 해결하는 방법을 아는 사람이 있는지 듣고 싶습니다. 아마도 최신 비주얼 스튜디오의 설정일까요 ...?

불쾌하지만 실행 가능한 솔루션 (제 경우)은 내 사용자 정의 예외 를 던진 다음 Stephen Cleary의 대답을 수정하는 것입니다.

디버그 메뉴에서 예외 (이 바로 가기 키 Control+ Alt+를 사용할 수 있음 E) ...를 선택합니다. 예외 대화 상자에서 공용 언어 런타임 예외 줄 옆에있는 throw 상자를 선택합니다.

더 구체적으로 말하자면, 목록에 사용자 지정 예외를 추가 한 다음 "Thrown"상자를 선택합니다.

예 :

async static Task AsyncTaskOp()
{
    await Task.Delay(300);
    throw new MyCustomException("Exception in async method");
}

익명의 델리게이트를 Task.Run(() =>.

Task.Run(() => 
{
     try
     {
          SyncOp());
     }
     catch (Exception ex)
     {
          throw;  // <--- Put your debugger break point here. 
                  // You can also add the exception to a common collection of exceptions found inside the threads so you can weed through them for logging
     }

});

참조 URL : https://stackoverflow.com/questions/18084983/debugger-not-breaking-stopping-for-exceptions-in-async-method

반응형