尝试在C#中捕获以除以零错误 [英] Try catch in c# for divide by zero error

查看:74
本文介绍了尝试在C#中捕获以除以零错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to do a try catch in c# so that I execute a sql query inside the try catch

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

Sometimes the value of count is 0, and it throws a error divide by zero error. So when it throws the divide by zero error I have to execute a sql statement to delete that statement and and the loop has to continue to get the value of the next record. How can i do it.

double value = (read * 100 / count);

推荐答案

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

Why doing a try/catch when you can simply test whether the value of count is equal to 0:

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);
}

这篇关于尝试在C#中捕获以除以零错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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