Rub*_*ans 5 c# exception-handling
嗨,我想知道你对异常处理的看法是什么,即我有一个方法:
public void Method{}
{
for (int i=0;i < length )
{
// dosomething that may case exception
...
...
// rest of the code
}
}
Run Code Online (Sandbox Code Playgroud)
我应该为整个循环添加try catch块以进行异常处理,还是只添加最有价值的代码或其他东西?什么是最佳做法?
答案是你想要/可以处理它的级别.如果一个元素的处理失败但你可以继续处理,那么使用try catch inside循环.如果错误可能发生并且您无法继续,则使用外部try catch.