为什么在 C# 中处理相同的异常时会出现两种不同的结果?
我在两个程序中处理了相同的异常,但得到了不同的结果。我不知道为什么。
第一个结果是程序无法捕获的结果
Scapshot1: www.freeimagehosting.net/uploads/e2b37433a3.png
第二个是程序成功捕获的
Scapshot2: www.freeimagehosting.net/uploads/6ab7564999.png
为什么我的结果如此不同?
I handled the same exception in two programs, but I got the different results. I don't know why.
the first result is the one the program unable to caught
Scapshot1: www.freeimagehosting.net/uploads/e2b37433a3.png
and the second is the one the program succeed to caught
Scapshot2: www.freeimagehosting.net/uploads/6ab7564999.png
Why I got such a different?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,你并没有真正提供太多背景信息。要检查的事项:
Well, you haven't really provided much context. Things to check:
据我所知,在#1 中,您正在调试器中运行。在#2 中你不是。
事实上,我会假设异常是完全相同的,因为消息是相同的。没有什么区别,IOW。
From what I can see, in #1 you are running in the debugger. In #2 you are not.
In fact, I will assume the exception is exactly the same, as the message is the same. There is no difference, IOW.
我不确定我是否理解,看起来是相同的错误,但
请提供更多信息
I am not sure if I understand, it looks like it's the same erro but
please provide more information
面对异常时调试器的行为可能会有很大差异。
异常处理(调试)
The behaviour of the debugger in the face of exceptions can vary wildly.
Exception Handling (Debugging)
调用 Application.Run() 后无法更改 Application.UnhandledExceptionMode,因此显然这不是捕获异常的方法。
这就留下了 AppDomain.UnhandledException。是的,调试器将在引发该事件之前中断异常。不错的功能,允许您调试异常原因。只需按 F5 继续执行即可触发事件处理程序。在第二个屏幕截图中没有证据表明您使用了调试器,看起来您是通过 Ctrl+F5 启动的。
You cannot change Application.UnhandledExceptionMode after Application.Run() was called, so clearly that's not the one that's going to catch the exception.
Which leaves AppDomain.UnhandledException. Yes, the debugger will break on the exception before that event is raised. Nice feature, allows you to debug the exception reason. Just press F5 to continue execution to trigger the event handler. There's no evidence of you using the debugger in the 2nd screen shot, looks like you started it with Ctrl+F5.