在try-catch块中进行调试->有缺陷的代码在哪里? [英] Debugging within try-catch blocks -> where is the flawed piece of code?

查看:113
本文介绍了在try-catch块中进行调试->有缺陷的代码在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



自从我们的程序员离开团队以来,我现在负责维护我们的业务工具(实际上不是程序员).该工具在历史上已经发展壮大,因此有时代码很混乱.它由封装在级联try catch块中的巨大方法组成.

我知道尝试catch块是最佳实践,但是使用巨大的方法会造成一些严重的麻烦:如果程序崩溃(在调试时),它不会停留在有缺陷的代码段上,而只会停留在catch块上.因此,除了通常没有那么多信息的错误消息外,我还没有真正的方法来说明try块中的巨大代码段的哪一部分导致了问题.

到目前为止,我发现的唯一解决方法是非常乏味的:注释掉所有try-catch环境",让程序运行并查看崩溃的地方.如果没有try-catch块,它将在错误的行处停止.

现在,这不是真的吗?我必须确定,即使try-catch块处于活动状态,也必须有一种方法可以查看导致崩溃的代码行.我只是想不通,也找不到关于该主题的任何有用信息.

任何见解都将不胜感激.

非常感谢!
-Pesche

Hi,

since our programmer has left the team, I am now responsible for the maintenance of our business tool (without being a programmer, really). The tool has historically grown and hence the code is quite messy at times. It consists of huge methods that are encapsulated in cascaded try catch blocks.

I know try catch blocks are best practice, but with huge methods they cause some serious trouble: If the program crashes (while debugging), it won''t stop at the flawed piece of code, but rather in the catch block. So, apart from the often not so informative error message, I have no real way of telling which of part of the huge code segment within the try block caused the problem.

The only workaround I''ve figured out so far is quite tedious: Comment out all the try-catch "surroundings", let the program run and see where it crashes. Without try-catch blocks, it stops right at the erroneous line.

Now, this can''t really be it, can it? There must be a way to see which line of code caused the crash even when the try-catch block is active, I am sure of it. I just can''t figure out how, and can''t google any useful information on that topic, neither.

Any insight is greatly appreciated.

Thank you very much!
- Pesche

推荐答案

轻松自如.

在Visual Studio菜单栏上查看:在调试"菜单下,您将找到异常".在出现的对话框中,在抛出"和用户未处理"下面的每个复选框中打勾. (您可能想记下打勾的那些),请按OK.

现在,如果您在调试器中运行您的应用程序(通常是F5键),则在引发异常时,它将停止运行,无论是否要处理该异常.对于一个编写良好的程序来说,这只是您需要找到的例外-但是有些程序不进行检查,并将例外视为流控制方法...如果是其中一种,那么我就可以了''抱歉!
Easy peasy.

Look on the Visual Studio menu bar: under the DEBUG menu, you will find "Exceptions". In the dialog that comes up, put a tick in every single check box, both under "thrown" and "User unhandled". (You might want to note which ones are ticked to start with) Press OK.

Now, if you run your app in the debugger (F5 is normally the key) it will stop whenever an exception is thrown, regardless of if it is going to be handled. With a well written program, this will just be the exception you need to find - but there are programs that don''t check, and treat exceptions as a flow control method...If it is one of those, then I''m sorry for you!


除了Griff和Simon的好建议之外,我还建议另外一个方法:如果您的调试执行在异常处理程序块中设置的断点处停止,并且您无法查看在何处引发了异常,可以检查当前捕获的异常的属性System.Exception.Stack.如果程序集是在Debug配置中编译的,则除了完整的方法名称之外,异常堆栈还将显示文件名和行号.这也是放入异常转储的好信息:异常堆栈只是人类可读的字符串.包含System.Exception.InnerException通常很有用.由于此属性也是System.Exception类型,因此您可能需要递归检查所有内部异常链.

—SA
In addition to the good advice by Griff and Simon, I would advise one more recipe: if your debug execution stops at a breakpoint set in an exception handler block and you cannot see where the exception was thrown, you can examine the property System.Exception.Stack of currently caught exception. If the assembly is compiled in Debug configuration, exception stack will also show file names and line numbers, in addition to the full method names. This is also a good information to put in exception dump: exception stack is just a human-readable string. It''s often useful to include System.Exception.InnerException. As this property is also of the type System.Exception you may want to check up all the chain of inner exception, recursively.

—SA


您有两个选择,
1)使用#if DEBUG,以便在调试时不存在try-catch(-finally)
2)告诉Visual Studio在引发异常时中断.您可以通过在菜单中的调试"下单击异常..."来执行此操作.

我更喜欢2
You have two options as I see it
1) use #if DEBUG so the try-catch(-finally) is not present when debugging
2) Tell Visual Studio to break when the exception is thrown. You do this by clicking "Exception..." under Debug in the menu.

I prefer 2


这篇关于在try-catch块中进行调试->有缺陷的代码在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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