简体   繁体   English

图书馆吞噬了我的例外

[英]Library eats up my exceptions

I'm using a GUI library like this (pseudocode): 我正在使用这样的GUI库(伪代码):

static void ProcessFrame()
{
    // ...
    if(fooWentWrong) throw new FooException();
    if(barWentWrong) throw new BarException();
    // ...
}

static void Main()
{
    GUILib.OnEveryFrame += delegate { ProcessFrame(); };

    try
    {
        GUILib.MainLoop();
    }
    catch(FooException e) // we don't handle BarException
    {
        // handle exception.
    }
}

In other words, GUILib is using inversion of control - its mainloop calls my delegate. 换句话说,GUILib正在使用控制反转-它的主循环调用我的委托。

The problem is that GUILib is a legacy lib that catches any exception thrown by OnEveryFrame handlers and just prints it to the console, so my FooException handler is not reached for FooExceptions, and for BarExceptions the Visual Studio debugger doesn't break. 问题在于GUILib是一个旧版库,可捕获OnEveryFrame处理程序引发的任何异常并将其仅打印到控制台,因此FooExceptions不会到达我的FooException处理程序,而对于BarExceptions,Visual Studio调试器不会中断。

I have made the debugger break for BarExceptions by going to Debug -> Exceptions and checking the break on thrown (and not just "break on onhandled") box for "Common Language Runtime exceptions". 我通过转到Debug- > Exceptions并检查“ Common Language Runtime exceptions” 引发中断 (而不仅仅是“ on onhandled中断”)框来使BarExceptions调试器中断。 This works, but it means VS now breaks when any exception is thrown, even when a handler would have caught it. 这行得通,但是这意味着VS现在会在抛出任何异常时中断,即使处理程序会捕获到它也是如此。 So I can't use exceptions for non-fatal stuff. 所以我不能对非致命的东西使用例外。

Any better way to do make VS break on my exceptions? 还有什么更好的方法可以使VS打破我的例外? I need to somehow work around the library's broken behavior. 我需要以某种方式解决图书馆的损坏行为。

BTW, the library is qt4dotnet / Qt Jambi. 顺便说一句,该库是qt4dotnet / Qt Jambi。

You should check if GUILib has an event for 'unhandled' exceptions. 您应该检查GUILib是否具有“未处理的”异常事件。

The other approach is triggering the debugger yourself: 另一种方法是自己触发调试器:

     System.Diagnostics.Debugger.Break();

Instead of or in addition to throwing the Exception. 代替或除了引发Exception外。

Depending on the exceptions you're interested in, you are probably on the right track by using the Debug->Exceptions dialog, except that you're ticking all the CLR exceptions, so it catches everything. 根据您感兴趣的异常,使用Debug-> Exceptions对话框可能会走上正确的路,只是您勾选了所有 CLR异常,因此它捕获了所有内容。

You can filter it better: click the [+] icon next to the CLR exceptions entry to unfold it and it'll list them all individually, and then you can tick just specific ones - or do it the other way, and tick the main one to enable catching all exceptions in the debugger, then untick a few specific ones (eg MyInternalNotReallyAnExceptionButJustAStatusCodeException* :-). 您可以对其进行更好的过滤:单击CLR例外条目旁边的[+]图标以展开它,然后将其全部列出,然后您可以勾选特定的-或相反,然后勾选主要一个可以在调试器中捕获所有异常,然后取消选中一些特定的异常(例如MyInternalNotReallyAnExceptionButJustAStatusCodeException * :-)。 To easily find one or two specific exceptions, click Find... and type in a partial name - for example "NullR" will find the NullReferenceException. 要轻松查找一个或两个特定的异常,请单击“查找...”并键入部分名称-例如,“ NullR”将找到NullReferenceException。

* (And yes, I dislike the idea of using exceptions to report status information - IMHO exceptions should primarily be used to pass on a failure that can't be handled at the site of the throw) *(是的,我不喜欢使用异常来报告状态信息的想法-IMHO异常应主要用于传递无法在抛出站点处理的故障)

Is it possible to move the exception handling into the delegates? 是否可以将异常处理移入委托中? It might end up with a bit of redundant code, but you should be able to refactor the exception handling bodies into a function. 它可能最终会带有一些冗余代码,但是您应该能够将异常处理主体重构为一个函数。

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

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