ArithmeticException
KW.java
class KW
{
    public static void main(String args[])
    {
        int n=515, d=0;
        int result=n/d;
        System.out.println("Division "+result);
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW Exception in thread "main" java.lang.ArithmeticException: / by zero at KW.main(KW.java:6) kodingwindow@kw:~$
Java program to handle the ArithmeticException
KW.java
class KW
{
    public static void main(String args[]) 
    {
        int n=515, d=0;
        try
        {
            int result=n/d;
        }
        catch(ArithmeticException e)
        {
            System.out.println("Can't divide by zero");
        }
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW Can't divide by zero kodingwindow@kw:~$
Advertisement