Saturday, 17 September 2011

Exception handling demo in java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


class Math
{
    public static int sum(int x,int y) throws NumberFormatException
    {
        return x+y;
    }
   
    public static int div(int x,int y) throws ArithmeticException
    {
        return x/y;
    }
}




public class DivTesting
{
    public static void main(String[] args)
    {
        BufferedReader buffer = null;
        int x;
        int y;
       
       
       
        try
        {
            buffer = new BufferedReader(new InputStreamReader(System.in));
           
            System.out.print("X is : ");
            x = Integer.parseInt(buffer.readLine());
           
            System.out.print("Y is : ");
            y = Integer.parseInt(buffer.readLine());
           
            System.out.println("Sum is : " + Math.sum(x,y));
           
           
            System.out.println("Div is : " + Math.div(x, y));
           
        }
       
        catch(Exception exception)
        {
            System.out.println("Exception : " + exception);
        }
   
   
    }
}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.