简体   繁体   English

为什么我无法捕获第三方库引发的异常?

[英]Why I can't catch an exception thrown from third party library?

Using using third party library I experience interesting situation. 通过使用第三方库,我遇到了有趣的情况。 The following code breaks on exception: 以下代码在异常时中断:

var instance = new Class(arg);

But when this line is enclosed in try/catch block, the exception is never caught. 但是,当此行包含在try/catch块中时,永远不会捕获异常。

Of course, the visual studio debugger stops on the exception only when the break on given exception type is enabled. 当然,只有启用了对给定异常类型的中断后,Visual Studio调试器才会在异常上停止。 When disabled, the exception disappears (at all). 禁用后,异常消失(完全消失)。 The catch block is never executed. catch块永远不会执行。 Does not matter if catch (Exception exc) {} or catch {} is used. 使用catch (Exception exc) {}还是catch {}并不重要。 The exception is derived from Exception . 异常是从Exception派生的。

How is this possible? 这怎么可能?

I suppose this might be common trick or practice to have "debug only" exceptions. 我想这可能是“仅调试”异常的常见技巧或做法。 The third party library uses code like this: 第三方库使用如下代码:

public class Class
{
    public Class(object arg)
    {
        try
        {
           ...
           throw new Exception("message");
           ...
        }
        catch
        {
            // This is just empty. By purpose.
        }
        finally
        {
            ...
        }
    }
}

The debugger then stops on the throw statement (if configured to do so), but as the exception is "handled", it does not propagate anywhere else... 然后,调试器将在throw语句上停止(如果已配置为这样做),但是由于“已处理”异常,因此它不会传播到其他任何地方...

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

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