The uncatchable exception, pt 2

前端 未结 2 1870
无人共我
无人共我 2021-02-07 01:21

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

相关标签:
2条回答
  • 2021-02-07 02:03

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

    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!");
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-07 02:10

    You can't catch all exceptions. There's a few assumptions in your example. You are, for instance, assuming the exception was raised on the calling thread. Catching unhandled exceptions on other threads depends on which runtimes you're using (console, winforms, WPF, ASP.Net, etc).

    Additionally, calls to System.Environment.FailFast() do not generate any handlable condition - the process is effectively terminated with no chance for intervention.

    0 讨论(0)
提交回复
热议问题