什么是最好的选择"在错误恢复下一步]对于C#? [英] What is the best alternative "On Error Resume Next" for C#?

查看:90
本文介绍了什么是最好的选择"在错误恢复下一步]对于C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我把空的catch块我的C#代码,它是否将成为VB.NET的论的错误恢复下一步语句中的等价物。

If I put empty catch blocks for my C# code, is it going to be an equivalent for VB.NET's "On Error Resume Next" statement.

try
{
    C# code;
}

catch(exception)
{
}

我问这个的原因是因为我要转换的VB.NET代码为C#,和旧的代码有〜200上的错误继续下一步虽然我使用的是正确的<$ C $声明C>尝试{}赶上{} 在我的新代码,但有一个更好的选择?

The reason I am asking this is because I have to convert a VB.NET code to C#, and the old code has ~200 "On Error Resume Next" statements although I am using a proper try {} catch {} in my new code, but is there is a better alternative?

推荐答案

我发现,VB程序员往往散落代码有许多上的错误继续下一步语句了(坏)习惯。我的建议是用抑制异常启动,看看有什么实际上打破。你觉得有可能不会像许多问题。相反,更多的回归测试,你可以做,效果更好;可能有一些优势情况下,只有当错误被忽略工作。

I've found that VB programmers often littered code with many On Error Resume Next statements out of (bad) habit. My suggestion would be to start with no suppressed exceptions, and see what actually breaks. There may not be as many issues as you think. Conversely, the more regression testing you can do, the better; there may be some edge cases that only work when errors are ignored.

最后,你需要在一个错误处理策略决定,无论是众多的尝试里面优美的平仓/赶块,或者让错误的渗透到顶层处理(这两种策略都有其用途)。

Ultimately, you need to decide on an error handling strategy, whether it is graceful unwinding inside many try/catch blocks, or letting errors percolate to a top-level handler (both strategies have their uses).

如果你最终不得不取消一些例外,以满足最后期限, 最起码记录这些异常,所以,未来开发商在你的代码的工作没有得到通过烧空的try / catch

If you end up having to suppress some exceptions to meet a deadline, at the very least log those exceptions so that the next developer working on your code doesn't get burnt by an empty try/catch.

这篇关于什么是最好的选择&QUOT;在错误恢复下一步]对于C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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