16 Jan 2017

How to handle Airhtmetic Exception divide by zero..??

Hello friends today i will show you how to handle Arithmetic Exception using try and catch block:


First of all , i will write a  program with Arithmetic Exception error :]
in this example i will assign three variable a, b and c. We divide a with b (0) , the int data type used to store the values .

class remote
{
    public static void main(String s [])
    {
        int a=10, b=0, c;
        c=a/b;

        System.out.println(c);
        c=a+b;

        System.out.print(c);
    }
}


 if you compile this program you cannot get any error but if you execute this program you will see the error:
     
         Exception in thread "main" java.lang.ArithmeticException: / by zero
        at remote.main(remote.java:6)



to handle this exception you need to use try catch block :
 


class remote
{
    public static void main(String s [])
    {
        int a=10, b=0, c;
        try
        {
           c=a/b;
            System.out.print(c);
        }
        catch(ArithmeticException m)
        {
       
        System.out.println("You cannot divide "+a+" by "+b);

        }
       
        c=a+b;

        System.out.print(c);
    }
}
you have solved the exception error . Now you think why you we use the exception handling case because of stop statement if we get any exception in any line , so we use this 

.

If you have any problem in this program you can comment on this blog and thank you for reading this blog...😃😃😃😃😃😃



















No comments:

Post a Comment