Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Try catch in c# for divide by zero error

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

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);
like image 947
Mark Avatar asked Nov 27 '22 23:11

Mark


1 Answers

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);
}
like image 143
Darin Dimitrov Avatar answered Nov 29 '22 11:11

Darin Dimitrov