即使异常被处理,在VS2010调试器中得到一个未处理的异常 [英] Getting an Unhandled Exception in VS2010 debugger even though the exception IS handled

查看:143
本文介绍了即使异常被处理,在VS2010调试器中得到一个未处理的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有VS2010的问题,调试器停止与未处理的异常。但是,这个例外是绝对处理的。实际上,如果我把代码放在catch块中,我按F5键就会打开它。在调试 - >异常中,我绝对不要 选中Thrown复选框,所以IMO绝对没有任何原因可以弹出未处理的异常对话框...

I have an issue with VS2010 where the debugger stops with an Unhandled Exception. However, the exception is definitely handled. In fact, if I put code in the catch block, I'll hit it when I press F5. In Debug -> Exceptions, I definitely do not have the "Thrown" checkbox checked, so IMO there is absolutely no reason for the unhandled exception dialog to pop up...

我不能发布确切的代码,但会很快在一个示例上工作。违规代码部分背后的基本思想是,我有一个与硬件相关的线程,如果我有一个错误的话,我会抛出一个 HardwareException 。线程以 BeginInvoke 启动,当我调用 EndInvoke 时,该回调处理程序中会捕获该异常。

I can't post the exact code, but will work on a sample soon. The basic idea behind the offending code section is that I have a thread that talks to hardware, and if I have an error talking to it, then I throw a HardwareException. The thread is launched with BeginInvoke, and the exception is caught in the callback handler when I call EndInvoke.

当调试器中抛出异常时,我收到一个消息框,显示HardwareException不由用户代码处理,但它是!!!

When the exception is thrown in the debugger, I get a messagebox that says 'HardwareException not handled by user code". But it is!!!

编辑 - 嗯,这让我很疯狂,我有一些示例代码,代表我在应用程序中的代码,它看起来像这样:

EDIT -- Well, this is driving me crazy. I've got sample code that is representative of the code I have in my application, and it looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting.Messaging;
using System.Threading;

namespace ConsoleApplication1
{
    public class HardwareException : ApplicationException
    {
        public HardwareException( string message) : base(message) {}
    }

    class Program
    {
        delegate void HardwareTestDelegate();

        static void Main(string[] args)
        {
            HardwareTestDelegate d = new HardwareTestDelegate( HardwareTestThread);
            d.BeginInvoke( HardwareTestComplete, null);
            while( true);
        }

        static void HardwareTestThread()
        {
            throw new HardwareException( "this is a test");
        }

        static void HardwareTestComplete( IAsyncResult iar)
        {
            try {
                AsyncResult ar = (AsyncResult)iar;
                HardwareTestDelegate caller = (HardwareTestDelegate)ar.AsyncDelegate;
                caller.EndInvoke( iar);
            } catch( Exception ex) {
                Console.WriteLine( "Should see this line without getting an unhandled exception message in the IDE");
            }
        }
    }
}

从线程抛出我的HardwareException,然后调用EndInvoke时处理异常。我猜Murphy是对的,因为当我运行这个示例代码时,它会执行我的预期 - 即没有未处理的异常错误消息在IDE中弹出!

I throw my HardwareException from the thread, and then handle the exception when EndInvoke is called. I guess Murphy was right, because when I run this sample code, it does what I expect -- i.e. no unhandled exception error message pops up in the IDE!

推荐答案

这是Microsoft的答复,案例为111053102422121. Allen Weng 写下:

Here is the response from Microsoft, case 111053102422121. Allen Weng writes the following:

对于您的信息,当您调用EndInvoke()时,CLR将在回调内重新抛出异常。以下是EndInvoke()的简化版本:

For your information, CLR will re-throw the exception inside the callback when you call EndInvoke(). Below is a simplified version of EndInvoke():

public object EndInvoke(IAsyncResult asyncResult)
{
    using (new MultithreadSafeCallScope())
    {
        ThreadMethodEntry entry = asyncResult as ThreadMethodEntry;
         ............
        if (entry.exception != null)
        {
            throw entry.exception;
        }
     }
}

回调函数或异步方法,如果提供异常处理程序。这是没有调试器附加的工作原理。

The exception will be handled in the call back function or in the asynchronous method if an exception handler is provided. This is how it works without a debugger attached.

当您在VS.NET中运行它时,调试器似乎仅检查异步方法中异常处理程序的存在。如果没有这样的处理程序,调试器会认为异常没有被处理,并弹出一条错误消息通知你。

When you run it in VS.NET, the debugger seems checking only the presence of the exception handler in the asynchronous method. If there is no such handler, the debugger would think the exception is not handled and pop up an error message notifying you of this.

当应用程序独立运行时,应用程序应按预期工作。如果错误消息在调试中令人烦恼,可以通过在异常对话框(调试|异常或按Ctrl + ATL + E)中选中常用语言运行时异常的未处理用户来禁用它。或者您可以在异步方法中添加try / catch。在后一种情况下,异常设置为null,并且不会在EndInvoke()中重新抛出异常。

The application should work as expected when you run it stand alone. If the error message is annoying in debugging for you, you can disable it by unchecking "User unhandled" for "Common Language Runtime Exceptions"in the Exception dialog box (Debug|Exceptions or press CTRL+ATL+E). Or you can add try/catch in the asynchronous method. In the latter case, the exception is set to null and won’t be re-thrown in EndInvoke().

这篇关于即使异常被处理,在VS2010调试器中得到一个未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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