gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 18:48:21 26 4
gpt4 key购买 nike

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

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

double value = (read * 100 / count);

最佳答案

当您可以简单地测试 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);
}

关于c# - 尝试在 c# 中捕获除以零错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8093919/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com