在无法捕获的异常,PT 2 [英] The uncatchable exception, pt 2

查看:112
本文介绍了在无法捕获的异常,PT 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:我提起过Microsoft Connect上的bug报告:的 https://connect.microsoft.com/VisualStudio/feedback/details/568271/debugger-halting-on-exception-thrown-inside-methodinfo-invoke#details

Update: I've filed a bug report on Microsoft Connect: https://connect.microsoft.com/VisualStudio/feedback/details/568271/debugger-halting-on-exception-thrown-inside-methodinfo-invoke#details

如果你可以在你的机器上重现此问题,请给予好评的bug,因此它可以是固定的!

If you can reproduce this problem on your machine, please upvote the bug so it can be fixed!

好吧,我已经做了一些测试,我已经减少了问题的东西很简单:

Ok I've done some testing and I've reduced the problem to something very simple:

我。在抛出异常的新类中创建一个方法:

i. Create a method in a new class that throws an exception:

public class Class1 {
    public void CallMe() {
        string blah = null;
        blah.ToLower();
    }
}



二。创建一个指向该方法在其他什么地方的MethodInfo:

ii. Create a MethodInfo that points to this method somewhere else:

Type class1 = typeof( Class1 );
Class1 obj = new Class1();
MethodInfo method = class1.GetMethod( "CallMe" );



三。包装一个来调用()在try / catch块:

iii. Wrap a call to Invoke() in a try/catch block:

try {
    method.Invoke( obj, null ); // exception is not being caught!
} catch {
}



四。没有调试器(正常工作)运行程序。

iv. Run the program without the debugger (works fine).

诉现在运行与调试程序。发生异常时调试器将停止该程序,即使它包裹在试图忽略它的catch处理函数。 (即使你把一个断点在catch块到达之前,将停止!)

v. Now run the program with the debugger. The debugger will halt the program when the exception occurs, even though it's wrapped in a catch handler that tries to ignore it. (Even if you put a breakpoint in the catch block it will halt before it reaches it!)

<击>其实,当你不运行的异常情况发生调试了。在一个简单的测试项目它在其他一些级别越来越忽略,但如果你的应用程序有任何形式的全局异常处理,它会被触发那里。 [查看评论]

这是害我一个真正的难题,因为它保持引发我的应用程序的崩溃处理程序,更不要说痛苦,是试图调试。

This is causing me a real headache because it keeps triggering my app's crash-handler, not to mention the pain it is to attempt to debug.

推荐答案

我可以重现这对我的.NET 4中框,你是对的 - 它只是发生在.NET 4.0

I can reproduce this on my .NET 4 box, and you're right -- it only happens on .NET 4.0.

这闻起来很像我的一个bug,应继续下去MS连接。 主要令人失望,如果这是你的跳闸崩溃处理。听起来像一个非赏心悦目的方式来解决,这是包装自己的处理程序内调用的方法。 : - (

This smells very much like a bug to me, and should go on MS Connect. Major bummer if this is tripping your crash handler. Sounds like a non-pleasing way to work around this is to wrap the invoked method inside its own handler. :-(

有一件事我不能重现,虽然是跳闸崩溃处理这是我的计划:

One thing I can not reproduce, though, is tripping the crash handler. Here's my program:

namespace trash {
    public class Class1 {
        public void CallMe() {
            string blah = null;
            blah.ToLower();
        }
    }

    class Program {
        static void Main(string[] args) {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);           
            var class1 = typeof(Class1);
            var method = class1.GetMethod("CallMe");

            try {
                var obj = new Class1();
                method.Invoke(obj, null); // exception is not being caught!
            }
            catch (System.Reflection.TargetInvocationException) {
                Console.Write("what you would expect");
            }

        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {
            Console.Write("it would be horrible if this got tripped but it doesn't!");
        }
    }
}

这篇关于在无法捕获的异常,PT 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆