无法捕获的异常

发布于 2024-09-05 23:50:11 字数 1179 浏览 12 评论 0原文

后续: 不可捕获的异常,第 2 部分

我是编写自定义绑定引擎;在目标元素上设置 DataContext 之前调用我的转换器。这本身并不是什么大问题,因为当 DataContext 最终收到一个值时它会被更新。导致问题的是 NullReferenceException,因为 DataContext 为 null,这似乎不想被捕获。

尽管我试图捕获值转换器中的异常:

try {
    return ( (MethodInfo)_member ).Invoke( parameter, null );
} catch {
    return null;
}

由于某种原因,调试器此时仍然停止。

替代文本

(这将堆栈跟踪稍微备份到 catch 块所在的位置 - 实际的异常发生在 _member 方法内部。奇怪的部分是 catch 块显示为灰色,但从未到达断点。)

现在我认为这可能是因为异常发生在捕获它的另一个程序集中(我试图将其打包在可重用的类库中,上面的 _member 指向应用程序程序集中的方法)。

如果我在没有调试器的情况下运行我的小测试应用程序,它可以正常工作,但是我的应用程序更加健壮,并且具有因此而触发的一般异常处理。

我想知道是否只有某些属性或某些东西(或者可能是我缺少的某些反射参数?)我可以使用它来捕获异常,就像它应该的那样。

更新:我很确定这一定是由于MethodInfo.Invoke的反射和使用造成的。看起来该异常是第一个“TargetInitationException”,其内部异常为 NullReferenceException。看来调用异常以某种方式发生在调用堆栈之外,因此没有被捕获在调用堆栈内部。我没有对线程做任何事情,但也许 MethodInfo.Invoke 内部正在进行某种隐式线程转换?

有谁知道如何强制捕获此问题,或者也许有另一种方法从不会出现此问题的方法名称调用方法?我有点难住了。

Followup: The uncatchable exception, pt 2

I'm writing a custom binding engine; my converter is being called before DataContext is set on the target element. This in and of itself isn't a big deal because it will get updated when DataContext eventually receives a value. What is causing problems is the NullReferenceException I'm getting because of DataContext being null, that doesn't seem to want to be caught.

Even though I'm attempting to catch the exception in my value converter:

try {
    return ( (MethodInfo)_member ).Invoke( parameter, null );
} catch {
    return null;
}

For some reason the debugger is still halting at this point.

alt text

(This is backed-up the stack trace a bit to where the catch block is--the actual exception happens inside the _member method. The odd part is the catch block is greyed out yet the breakpoint is never reached.)

Now I'm thinking it could be because the exception is happening in another assembly from where it is being caught (I'm trying to package this in a reusable class library, and _member above points to a method in the application assembly).

If I run my little test app without the debugger it works fine, however my application is a little more robust and has general exception handling which is getting triggered because of this.

I'm wondering if there is just some attribute or something (or perhaps some reflection parameter I'm missing?) I can use to make the exception be caught like it's supposed to.

Update: I'm pretty sure this must be due to the reflection and use of MethodInfo.Invoke. It seems that the exception is first of "TargetInvocationException" with an inner exception of NullReferenceException. It seems the invocation exception is somehow occuring outside of the callstack and therefore isn't being caught inside it. I'm not doing anything with threads, but perhaps there is some kind of implicit thread-shifting going on inside MethodInfo.Invoke?

Does anyone have any ideas how I could force this to be caught, or perhaps another way to invoke a method from a method name that won't have this problem? I'm kind of stumped.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌上青苔 2024-09-12 23:50:11

检查调试器异常设置,看看您是否告诉调试器在引发 NullReferenceException 时中断。

Check in the debugger exception settings to see if you're telling the debugger to break when NullReferenceException is thrown.

怪我鬧 2024-09-12 23:50:11

我非常确定您可以在调用后很好地捕获异常,并且捕获异常不需要特定于反射的机制。

您调用的方法是否可能使用线程并在子线程中引发异常?如果异常发生在您离开 try-catch 语句之前尚未完成的异步线程中,这可能会导致您的 try-catch 错过异常。

I'm pretty sure you can catch exceptions just fine after an invoke and there's no reflection-specific mechanism required for catching it.

Is the method you're invoking perhaps using threading and throwing the exception in a child thread? This could cause your try-catch to miss the exception if the exception occurs in an anychronous thread that's not finished before you leave the try-catch statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文