尝试在c#中捕获除以零错误

Mar*_*ark 2 c# error-handling

如何在c#中尝试catch,以便在try catch中执行sql查询

有时count的值为0,它会将错误除以零错误.因此当它抛出除以零错误时,我必须执行一个sql语句来删除该语句,并且循环必须继续获取下一条记录的值.我该怎么做.

double value = (read * 100 / count);
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 29

当你可以简单地测试值count是否等于0 时,为什么要进行try/catch :

if (count != 0)
{ 
    // perform the division only if count is different than 0,
    // otherwise we know that it will throw an exception 
    // so why even attempting it?
    double value = (read * 100 / count);
}
Run Code Online (Sandbox Code Playgroud)

  • if(count> 0)会更好,负数(即使失败或错误类型)不是有效状态 (2认同)