C#异常仅调​​试时抓到? [英] C# Exceptions only caught when debugging?

查看:194
本文介绍了C#异常仅调​​试时抓到?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/2936947/exception-handling-problem-in-release-mode">Exception处理在释放模式问题

Possible Duplicate:
Exception handling problem in release mode

我怀疑是这是一个非常简单的解释,但我似乎无法找到它。

I suspect there is a perfectly simple explanation for this, but I can't seem to find it.

在我的WinForms C#4.0的应用程序加载本身Program.cs文件,整个main()函数在内部具有try / catch语句。

When my WinForms C# 4.0 application loads itself in the Program.cs file, the entire Main() function has inside it a try/catch statement.

我已经写了一个小的例外包装它非常类似的行为与香草.NET未捕获的异常中,但它提供了更多的信息,允许例外树被保存(序列化),它允许用户直接提交错误报告给我。

I have written a little exception wrapper which behaves quite similarly to the vanilla .net "uncaught exception" box, except it provides a bit more information, allows the exception tree to be saved (serialised), and it allows the user to submit the error report directly to me.

现在,它工作正常,在调试(F5)。如果我引发这在主线程,如果没有尝试程序异常的的任何地方的/搭上则异常触发的方式一路返回主(),并显示了自定义窗口。

Now, it works fine while debugging (F5). If I trigger an exception anywhere in the program which is in the main thread, if there is not try/catch then the exception fires its way all the way back to Main() and shows the custom window.

(所有其他异常我已经占了,并妥善处理)。

(All other exceptions I have accounted for and are handled appropriately).

当我通过运行.exe文件运行程序简单,香草的.NET异常对话框出现,不是我有codeD。

When I run the program simply by running the .exe file, the vanilla .net exception box comes up, not the one I have coded.

有什么理由,你能想到的,为什么会发生这种情况? 最奇怪的是在调试模式VS自身运行上运行时,它的表现完全不同。 我建立的调试 - 不松开

Is there any reason you can think of why this would happen? The strangest thing is that it behaves quite differently when running in debug mode vs running on its own. I am building as debug - not release.

编辑(22日至11):

Edit (22-March-11):

我只是加入了小编这里,在某些情况下,你不能找到隐藏在下面的接受的答案的评论答案: 忘了,我说我是建设作为调试,而不是释放。这是没有意义的 - 我只是把它添加额外的信息。最重要的是,当我调试的更改 VS中的异常引起的预期,但VS以外执行我的EXE的时候都没有。

I'm just adding a little addendum here, in case some of you can't find the answer hidden in the comments for the accepted answer below: Forget that I said I was building as debug instead of release. That is of no relevance - I just added it for extra info. What is important is that when I'm debugging in VS the exceptions are caught as expected, but when executing my EXE outside VS they aren't.

由于科迪说, Application.Run()都有自己的处理程序异常这就是为什么他们从来没有让我的主,但是我提到,我甚至不使用 Application.Run()在我的code的任何地方......而不是我的GUI是先用<推出code> Form.ShowDialog()。

As Cody said, Application.Run() has its own handler for exceptions which is why they never get to my main catch, however I mentioned that I am not even using Application.Run() anywhere in my code... instead my GUI is first launched with Form.ShowDialog().

我已经做了一些实验,并能确认 Form.ShowDialog()的行为同样是为 Application.Run()在异常的方法本身中进行处理。

I have done some experimentation, and can confirm that Form.ShowDialog() behaves the same was as Application.Run() in that exceptions are handled within the method itself.

推荐答案

这是预期的行为。

你看到的差别是应用程序正在运行附加调试器的结果。当您从Visual Studio中启动它,调试器会自动连接(当然,除非你选择开始不调试)。禁用内置的异常处理程序,这是一个负责向您展示的香草的.NET异常对话框。从外面VS没有附加调试器启动它,使内置的异常处理功能。 (请注意,这没有任何关系,在调试模式与发布模式下编译程序。)

The difference you see is a result of the application being run with the debugger attached. When you launch it from within Visual Studio, the debugger is automatically attached (unless, of course, you choose to "Start Without Debugging"). That disables the built-in exception handler, which is the one responsible for showing you the "vanilla" .NET exception dialog. Launching it from outside VS doesn't attach the debugger, leaving the built-in exception handling enabled. (Note that this has nothing to do with compiling your program in "Debug" mode versus "Release" mode.)

请参阅<一href="http://stackoverflow.com/questions/2936947/exception-handling-problem-in-release-mode/2937372#2937372">the接受的答案以此相关的问题了解详情。我不相信的VB.NET和C#的区别是有关在这种情况下。

See the accepted answer to this related question for more information. I do not believe the difference between VB.NET and C# is relevant in this case.

由于这个问题的答案中提到,还有的的一种方法来禁用内置的异常处理程序。但是,选择这样做之前,我建议重新考虑你的方法。而不是包裹整个方法在try-catch块,其中sounds像一个有点code气味我,你可能会考虑处理内置的 AppDomain.UnhandledException 事件。杰夫·阿特伍德张贴了一大篇这里code项目有关如何更换标准的.NET异常处理自己的对用户更友好的方法。他建议已经成为只有更优雅为更高版本的.NET FW的解决方案提高了如何使用 AppDomain.UnhandledException 处理事件。

As that answer mentions, there is a way to disable the built-in exception handler. But before choosing to do so, I recommend reconsidering your approach. Rather than wrapping your entire Main method in a try-catch block, which sounds like a bit of a code smell to me, you might consider handling the built-in AppDomain.UnhandledException event. Jeff Atwood posted a great article here on Code Project about how to replace the standard .NET exception handling with your own more user-friendly method. The solution he proposes has become only that much more elegant as later versions of the .NET FW have improved how the AppDomain.UnhandledException event is handled.

这篇关于C#异常仅调​​试时抓到?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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