简体   繁体   中英

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

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

That's conditional logic and shouldn't be handled by an exception. Perform a check on the value of count before the operation and execute the sql statement accordingly.

you can try like this..

this is sample example

namespace nsDivZero
{
    using System;
    public class DivZero
    {
        static public void Main ()
        {
            // Set an integer equal to 0
            int IntVal1 = 0;
            // and another not equal to zero
            int IntVal2 = 57;
            try
            {
                Console.WriteLine ("{0} / {1} = {2}", IntVal2, IntVal1, IntResult (IntVal2, IntVal1) / IntResult (IntVal2, IntVal1));
            }
            catch (DivideByZeroException e)
            {
                Console.WriteLine (e.Message);
            }
            // Set a double equal to 0
            double dVal1 = 0.0;
            double dVal2 = 57.3;
            try
            {
                Console.WriteLine ("{0} / {1} = {2}", dVal2, dVal1, DoubleResult (dVal2, dVal1));
            }
            catch (DivideByZeroException e)
            {
                Console.WriteLine (e.Message);
            }
        }
        static public int IntResult (int num, int denom)
        {
            return (num / denom);
        }
        static public double DoubleResult (double num, double denom)
        {
            return (num / denom);
        }
    }
}

Rather than using if statements or try/catch statements I prefer to add double.Epsilon to the divider. If working with floats you can also use float.Epsilon . It represents the smallest possible number greater than zero and it won't really affect your result. I use this when generating reports and charts

double value = (read * 100 / (count + double.Epsilon));

Instead of using a try catch in this situation would it not be better to check if the count is 0. Something along the lines of

if (count == 0)
{
    DeleteStatement();
    continue;
}

double value = (read * 100 / count);

This type of exception is called a RuntimeException in Java (I know this is C#), which means that it should be prevented in the first place, not caught in a catch. Just test that count is not 0.

if (count > 0)//as suggested by sasjaq { double value = (read * 100 / count); }

Seems like a good way to do it. Using try catch will potentially lower performance noticeably...

if the value that you are passing is 0 by any chance and you are doing a division on it, you can get around it just by doing the following:

int x = 0; 

int z = 1300; 

if divided by 0 you will pass infinity value else you pass the correct amount that you want based on the division!

int result = x == 0 ? 99999 : x / z; 

Console.WriteLine(result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM