简体   繁体   中英

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.

Of course, the visual studio debugger stops on the exception only when the break on given exception type is enabled. When disabled, the exception disappears (at all). The catch block is never executed. Does not matter if catch (Exception exc) {} or catch {} is used. The exception is derived from 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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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