简体   繁体   English

奇怪的异常处理实践

[英]Strange exception handling practice

I have seen code like this (actually seeing another person type it up): 我看过这样的代码(实际上是看到另一个人输入的代码):

catch (Exception ex)
{
    string exception = ex.ToString();
}

Is this code bad? 这个代码不好吗? If so, why? 如果是这样,为什么? There is an appropriate "chain of catch handlers (eg more specific one above, filtering down to general catch all Exception, but in the string conversion of the Exception, I guess you are converting a lot more than is probably needed, to a string (All you really need is the InnerMessage or one of the other string properties depending on the scenario). Any thing else wrong with this code? 有一个适当的“捕获处理程序链(例如,上面更具体的捕获处理程序,过滤到一般捕获所有Exception,但是在Exception的字符串转换中,我想您要转换成字符串的数量远远超过了所需的数量(您真正需要的只是InnerMessage或其他字符串属性之一(取决于情况)。此代码还有其他问题吗?

I have also seen devs put breakpoints on every line of code. 我还看到开发人员在每行代码上都设置了断点。 What is the point of this? 这有什么意义呢? Why not just put one at the top and then use "run to cursor" (love that feature)? 为什么不只是将一个放在顶部,然后使用“运行到光标”(喜欢该功能)呢?

Finally, what's the advantage of using break on all exceptions in Visual Studio? 最后,在Visual Studio中对所有异常使用break有什么好处?

   string exception = ex.ToString();

That doesn't DO anything. 那什么也没做。 Better to log it or use MessageBox.Show(...);. 最好记录它或使用MessageBox.Show(...);。

Breakpoints on each line ... not much point - use run to cursor or step over/step in. 每行的断点...点不多-使用运行光标或进入/进入。

Break on all exceptions: I've actually used. 打破所有例外:我实际上已经使用过。 I've had exceptions fail quietly that were "handled" by some library silently. 我曾经遇到过一些由某些库静默处理的异常,这些静默失败。 Break on all helped me track that down. 一切都中断了我。 Additionally "Break on all" can help you make sure you're only getting exceptions you expect (Also helped by not catching a generic "Exception" class but only catching the particular exception. 另外,“全部中断”可以帮助您确保仅收到您期望的异常(还通过不捕获通用的“ Exception”类而仅捕获特定的异常而有所帮助。

This looks like a lazy programmer who: 这看起来像个懒惰的程序员,他:

  1. Doesn't want to handle exceptions properly 不想正确处理异常
  2. Wants a spot to set a breakpoint if there's an exception. 希望有一个异常的地方设置断点。

The only value of the code you posted would be to permit the full exception to be visible while in the debugger. 您发布的代码的唯一价值是允许在调试器中看到完整的异常。 It's not necessary , since the debugger will do that anyway, but maybe this code was there since before the debugger did that. 这不是必需的 ,因为无论如何调试器都会这样做,但是也许这段代码是在调试器执行此操作之前就存在的。

This developer might not know that you can catch all (managed) exceptions like this ... 该开发人员可能不知道您可以捕获所有这样的(托管)异常...

try
{
  // do something
}
catch( Exception )
{

}

And not suffer the compiler warning of a catch block like this ... 并且不会遭受像这样的catch块的编译器警告...

catch( Exception ex )
{
  // don't use ex
}

Also, he might not know about the $exception pseudo-register . 另外,他可能不知道$ exception伪寄存器

I just used "break on all exceptions" yesterday. 昨天我只是使用“打破所有例外”。

I was chasing down a pretty obscure bug (in fact, the code was working perfectly, which is the hardest kind of bug of all to find), and while my C# code was executing an apparently-malfunctioning IronPython script, I kept getting ArgumentException messages appearing in the console. 我正在追查一个相当晦涩的错误(实际上,代码运行正常,这是所有错误中最难找到的一种),而当我的C#代码执行一个似乎功能异常的IronPython脚本时,我一直收到ArgumentException消息出现在控制台中。

It turns out that this IronPython code: 事实证明,此IronPython代码:

try:
    value += x
except ValueError:
    pass

results in an ArgumentException being thrown, and handled, inside the IronPython runtime. 导致在IronPython运行时内部引发和处理ArgumentException

Also, if you have "break on all exceptions" turned on, VS actually breaks on that value += x line, calls up the Python source, lets you inspect local values, etc. Pretty nice. 另外,如果启用了“打破所有异常”功能,VS实际上会在该value += x行上打破,调用Python源代码,让您检查局部值,等等。 Anyway, now when I see those exception messages show up in the console I no longer worry that I'm ignoring something that's going to bite me. 无论如何,现在当我看到那些异常消息显示在控制台中时,我不再担心我会忽略会咬我的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM