我对 VS2010 有一个问题,调试器因未处理的异常而停止。但是,肯定会处理异常。事实上,如果我将代码放在 catch 块中,我会在按 F5 时点击它。在 Debug -> Exceptions 中,我绝对没有选中“Thrown”复选框,因此 IMO 绝对没有理由弹出未处理的异常对话框......
我无法发布确切的代码,但很快就会制作一个示例。有问题的代码部分背后的基本思想是我有一个与硬件对话的线程,如果与它对话时出错,那么我会抛出一个HardwareException
. 线程是用 启动的BeginInvoke
,当我调用EndInvoke
.
当在调试器中抛出异常时,我收到一个消息框,上面写着“用户代码未处理硬件异常”。但它是!!!
编辑——嗯,这让我发疯了。我的示例代码代表了我的应用程序中的代码,它看起来像这样:
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 中不会弹出未处理的异常错误消息!