简体   繁体   English

如何在Visual Studio中忽略异步方法的特定异常INSTANCE(中断抛出)

[英]How to ignore a specific Exception INSTANCE for an async method in Visual Studio (break on thrown)

If I set VS to break on thrown exceptions of a given type, and the debugger haltet at some piece of code, I look for a possibility to continue running (F5) and not break on each parent stack frame of the same exception. 如果我设置VS来打破给定类型的抛出异常,并且调试器在某段代码中暂停,我会寻找继续运行(F5)的可能性,而不是同一异常的每个父堆栈帧上中断。

In other words, I want a possibility to ignore the very instance of the exception for the rest of its stackframe (assuming it is caught somewhere above). 换句话说,我希望有可能忽略其堆栈帧的其余部分的异常实例 (假设它被捕获到上面某处)。

The only way I found is tedious (and error-prone if such exceptions are thrown often in some situation): Uncheck the "break when this type is thrown" checkbox, continue execution, immediately reactivate the exception in the Exception Settings pane. 我发现的唯一方法是乏味(如果在某些情况下经常抛出此类异常,则容易出错):取消选中“抛出此类型时中断”复选框,继续执行,立即在“异常设置”窗格中重新激活该异常。


Edit: clarifying to 1st answer 编辑:澄清第一个答案

I forgot to mention (as I was not aware that this is relevant info) that I'm in async methods. 我忘了提及(因为我不知道这是相关的信息),我在异步方法。 I modified your example to async, like this: 我将您的示例修改为异步,如下所示:

    public static class Program
{
    public static void Main(string[] args)
    {
        try
        {
            Alpha();

        }
        catch (Exception e) // don't try this at home kids
        {
            // we should never get here in this example
            throw;
        }
    }


    private static async Task Alpha()
    {
        try
        {
            await Bravo(); //  *** 4 ***
        }
        catch (Exception e)
        {
            // debugger won't stop here because we didn't re-throw
        }
    }

    private static async Task Bravo()
    {
        try
        {
            await Tango(); //  *** 2 ***
        }
        catch (Exception)  // don't try this at home kids
        {
            throw;  // *** 3 *** debugger will stop here again because we are re-throwing
        }
    }

    private static async Task Tango()
    {
        var x = 1;
        var y = 0;
        var c = x / y; //  *** 1 ***

    }
}

And the debugger stops at all four marked spots in the numbered order. 并且调试器在编号顺序的所有四个标记点处停止。 So it does not only stop at rethrows, but also at all awaits inbetween. 所以它不仅停留在重新抛出,而且在所有等待中间停止。 And for deep call hierarchies, this is disturbing my debug-flow, so to speak. 对于深度调用层次结构,可以说这会扰乱我的调试流程。

So to summarize, I'd need a solution for async calls to not re-break at every await between the exception-causing stackframe and the one where the exception is eventually caught. 总而言之,我需要一个异步调用的解决方案,以便在导致异常的堆栈帧和最终捕获异常的堆栈帧之间的每次等待都不会重新中断。

Possible? 可能?


With the new info that became apparent in the question, this answer may no longer be suitable. 随着问题中显而易见的新信息,这个答案可能不再合适。 :) :)


If I set VS to break on thrown exceptions of a given type, and the debugger haltet at some piece of code, I look for a possibility to continue running (F5) and not break on each parent stack frame of the same exception.... 如果我设置VS来打破给定类型的抛出异常,并且调试器在某段代码上挂起,我会寻找继续运行(F5)的可能性,而不是在同一异常的每个父堆栈帧上中断... 。

The only way I found is tedious (and error-prone if such exceptions are thrown often in some situation): Uncheck the "break when this type is thrown" checkbox, continue execution, immediately reactivate the exception in the Exception Settings pane 我发现的唯一方法是单调乏味(如果在某些情况下经常抛出此类异常,则容易出错):取消选中“抛出此类型时中断”复选框,继续执行,立即在“异常设置”窗格中重新激活该异常

Without seeing your code I can only assume you are re-throwing the same exception. 没有看到你的代码,我只能假设你正在重新抛出相同的异常。

Consider the following code: 请考虑以下代码:

class Program
{
    static void Main(string[] args)
    {
        try
        {
            Alpha();

        }
        catch (Exception e) // don't try this at home kids
        {
            // we should never get here in this example
            throw;
        }

    }

    private static void Alpha()
    {
        try
        {
            Bravo();
        }
        catch (Exception e)
        {
            // debugger won't stop here because we didn't re-throw
        }
    }

    private static void Bravo()
    {
        try
        {
            Tango();
        }
        catch (Exception)  // don't try this at home kids
        {
            throw;  // debugger will stop here again because we are re-throwing
        }
    }

    private static void Tango()
    {
        var x = 1;
        var y = 0;
        var c = x / y;
    }
}

With the debugger set to break when System.DivideByZeroException is thrown, the debugger will: 抛出System.DivideByZeroException时调试器设置为中断,调试器将:

  1. stop first in Tango where it is attempts to divide by zero and the first exception is thrown 首先在Tango中停止尝试除以零,然后抛出第一个异常
  2. stop again in the catch() handler in Bravo where the exception is thrown again 再次在Bravo中的catch()处理程序中停止,再次抛出异常

But it will not stop in Alpha because I silently eat the exception. 但它不会停止在Alpha因为我默默地吃掉了这个例外。

The behaviour you are seeing is by design. 您看到的行为是设计的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM