使用C#我们可以在foreach
之外继续使用吗?我的意思是我们可以跳过异常情况并转到下一个记录验证?根据我的情况,我必须将代码重构为单独的方法,而不是在foreach
循环中使用continue。在foreach循环块外继续声明
foreach (var name in Students)
{
//forloop should continue even if there is any logic exception and move to next record
CheckIfStudentExist();
}
private void CheckIfStudentExist()
{
try
{
//do some logic to verify if student exist & take to catch if there is some error in logic
}
Catch(Exception)
{
continue;
}
}
如果逻辑中出现错误,请不要“catch”。您应该只捕获您无法编码的异常 - 例如磁盘空间不足或网络连接丢失。如果你编写自己的逻辑,你应该确保它是健壮的。尽可能避免'try' /'catch'。 – Enigmativity