Saturday 17 September 2011

User defined exception

class AgeException extends Exception
{
    private String message;
   
    public AgeException()
    {
        this.message = "Age Cannot Be Less Than 1";
    }
   
    public AgeException(String message)
    {
        this.message = message;
    }
   
    public String toString()
    {
        return this.message;
    }
}

class SetAge
{
    private int age;
   
    public void setAge(int age)
    {
        this.age = age;
    }
   
   
    public void showAge()
    {
        String message = "Age Cannot Be Greater Than 120";
       
        if(age >=1 && age <= 120)
        {
            System.out.println("Age Set : " + age);
        }
   
        else if ( age > 120)
        {
            throw new AgeException(message);
           
        }
        else
        {
            throw new AgeException();
        }
    }
}



public class AgeMain
{
    public static void main(String[] args)
    {
        SetAge setage = null;
       
        try
        {
            setage = new SetAge();
            setage.setAge(-5);
           
            setage.showAge();
           
        }
        catch(NullPointerException exception)
        {
            System.out.println("Exception Object Reference is Null ");
        }

        catch(AgeException exception )
        {
            System.out.println("Exceptio  : " +  exception);
        }
    }
}

IllegalArgumentException exeption


public class SetValues
{
    public static void main(String[] args)
    {
        try
        {
            System.out.println("Value : " + Integer.parseInt("4545"));
            System.out.println("Value : " + Integer.parseInt("60,40"));
        }
        catch(IllegalArgumentException exeption)
        {
            System.out.println("Exception : " + exeption);
        }
    }
}

IndexOutOfBoundsException exception in java


public class ArrayTesting
{
    public static void main(String[] args)
    {
        int arr[]= new int[2];
       
        arr[0]=1;
        arr[1]=2;
       
       
        int res;
       
       
        try
        {
            res = arr[0] + arr[1] + arr[2];
            System.out.println("Sum is : " + res);
        }
        catch(IndexOutOfBoundsException exception)
        {
            System.err.println("Exception : " +exception);
        }
       
       
    }
}

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