Wednesday, February 1, 2012

Complex code of Exception handeling in java

Q1)Output of the program
class except
{
  void hello(int a,int b)
  {
    try
    {
      System.out.println(a/b);
    }   
    catch(Exception e)
    {
     System.out.println("Exception caught" + e);
    }
  };
};

class main
{
 public static void main(String args[])
  {
    except e=new except();
    e.hello(10,0);   
  };
};

Output:-Exception caught
Discussion:-Value of b is 0. So when it tryes to devide by zero then the attempt to devide by zero exception occue.

Q2)Output of the program
class except
{
  void hello()
  {
    int a[]={1,2,3,4,5};
    try
    {
     for(int i=0;i<10;i++)
     System.out.println(a[i]);
    }   
   
    catch(Exception e)
    {
     System.out.println("Exception caught" + e);
    };
  };
};

class main
{
 public static void main(String args[])
  {
    except e=new except();
    e.hello();
  };
};
Output:-Exception caught.ArrayIndexOutOfBounds exception.
Discussion:-There are five element in a array.And in try block we are trying to print upto 10th element.
            So the array index out of bounds exception occur.
           
Q3)Output of the program.
class except
{
  void hello()
  {
    try
    {
     int a[]={1,2,3,'A','X'};
     for(int i=0;i<5;i++)
     System.out.println(a[i]);
    }   
   
    catch(Exception e)
    {
     System.out.println("Exception caught");
    };
  };
};

class main
{
 public static void main(String args[])
  {
    except e=new except();
    e.hello();
  };
};
Output:-1 2 3 65 88
Discussion:-We know character and integer are type compotaable.So for A and X character the corresponding ascii value is printed.
            No excepption has occured.           

Q4)Output of the program.
class except
{
  void hello()
  {
    try
    {
     char a[] = {'A','B'};
     for(int i=0;i<10;i++)
     System.out.println(a[i]);
    }   
    catch(Exception e)
    {
     System.out.println("Exception caught");
    }
    catch(StringIndexOutOfBoundsException exc)
    {
     System.out.println("String exception");
    };
  };
};

class main
{
 public static void main(String args[])
  {
    except e=new except();
    e.hello();
  };
};
Output:-Compile time error.
Discussion:-Here Exception is superclass of StringIndexOutOfBoundsException class.So all exception will catch by Exception class.
            And the StringIndexOutOfBoundsException    exception block will never exhicute.

Q5)Output of the program
class except
{
  void hello()
  {
    try
    {
      System.out.println(100/0);
    };   
    catch(ArithmeticException exc)
    {
     System.out.println("String exception");
    };
   
   
  };
};

class main
{
 public static void main(String args[])
  {
    except e=new except();
    e.hello();
  };
};
Output:-compile time error.
Discussion:-Try block has ended with ;.So compilar treated that there is no catch block.So error has occured.

Q6)Output of the prohram

class except
{
  void hello()
  {
    try
    {
      System.out.println(100/0);
    }   
    catch(ArithmeticException exc)
    {
     System.out.println("Arithmetic exception");
    }
    catch(Exception e)
    {
     System.out.println("Exception block");
    }
    finally
    {
     System.out.println("Finally Exhicuted");
    }
  };
};

class main
{
 public static void main(String args[])
  {
    except e=new except();
    e.hello();
  };
};
Output:-Arithmetic exception
        Finally Exhicuted
Discussion:-Here after exhicuting ArithmeticException block the finally block will exhicute.And the Exception block will not exhicute.

Q7)Output of the program.
class except
{
  void fun()
  {
        try
        {
         System.out.print(100/0);
        }   
        catch(ArrayIndexOutOfBoundsException e)
        {
         throw e;
        };
  };
};

class main
{
 public static void main(String args[])
  {
    try
    {
     except e=new except();
     e.fun();
    }
    catch(Exception e)
    {
      System.out.println("Re throw is caught");
    };
 };
};
Output:-Re throw is caught
Discussion:-When we re throw an object it search the another try catch ladder block.Here from fun() methode the object e is re throwing,
            and in main function another try catch block is caughting the exception.
           
Q8)Output of the program
class main
{
 public static void main(String args[])
  {
    try
    {
      int n=Integer.parseInt(args[0]);
      int m=Integer.parseInt(args[1]);
      int p=n+m;
     }
   
    catch(ArithmeticException e)
    {
     System.out.println("Arithmetic Exception caught");
    }
    catch(Exception e)
    {
     System.out.println("Exception caught" +e);
    };
 };
};
Output:-Run time error.
Discussion:-In this example.Array Index Out Of Bounds exception occur.And Exception block catch that exception.

Q9)Output of the program
class main
{
 public static void main(String args[])
  {
    try
    {
      int n=Integer.parseInt(args[0]);
      int m=Integer.parseInt(args[1]);
      System.out.println(n+m);
     }
   
    catch(ArithmeticException e)
    {
     System.out.println("Arithmetic Exception caught");
    }
    catch(Exception e)
    {
     System.out.println("Exception caught" +e);
    };
   
 };
};
Input:-java main 10 10
Output:-20
Discussion:-Here tow command line argument is passing as a main function paramiter.And using the rapper class it is converted to integer object.


Q9)Output of the program

class main
{
 public static void main(String args[])
  {
     if(args[0]=="hello")
      System.out.println("Valid string");
     else
      try
       {
            throw new Exception("Ivalid");
       }
      catch(Exception e)
       {
         System.out.println("Hello java");
       };      
  };
};
Output:-Output depend upon input.
Discussion:- If we pass one paramiter in command ling argument and if the paramiter is hello then It will print valid string.
             Otherwise It will print Hello java.
           
           
Q10)Output of the program
class main
{
 public static void main(String args[])
  {
    
       try
       {
            System.out.println(100/0);
       }
      
       catch(ArrayIndexOutOfBoundsException e)
       {
         System.out.println("Array exception");
       }      
       catch(ArithmeticException e)
       {
         System.out.println("Array exception");
       }      
      
       finally
       {
        System.out.println("Finally block");
       }
      
       catch(Exception e)
       {
         System.out.println(e);
       }
  };
};
Output:-Compile time error.
Discussion:-finally block should come at last of try catch block.


  
       
           

No comments:

Post a Comment