Tuesday, 30 October 2012

Custome Exception Handling Using Java

This is a program to illustrate custom exception handling in java

import java.io.*;

class MyException extends Exception
{
    MyException(int i)
    {
        if(i==0)
            System.out.println("Division by zero");
    }
    MyException(int i,int j)
    {
        if(j>i)
            System.out.println("Dinominator greater than nominator");   
    }
}

class ExceptionDemo
{
    int a,b;
    public static void main(String args[])
    {
       
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        try
        {
        a=Integer.parseInt(br.readLine());
        b=Integer.parseInt(br.readLine());
       
            if(b==0)
                throw new MyException(a);
            else if(b>a)
                throw new MyException(a,b);
                             else
                System.out.println("Result= " + a/b );
        }
        catch(Exception ex)
        {
            System.out.println("Error " + ex.getMessage());
        }
    }
}

No comments:

Post a Comment