Exception Handling


1
Question 1
Predict the output of following Java program
class Main {
   public static void main(String args[]) {
      try {
         throw 10;
      }
      catch(int e) {
         System.out.println("Got the  Exception " + e);
      }
  }
}
A
Got the Exception 10
B
Got the Exception 0
C
Compiler Error
Exception Handling    
Discuss it


Question 1 Explanation: 
In Java only throwable objects (Throwable objects are instances of any subclass of the Throwable class) can be thrown as exception. So basic data type can no be thrown at all. Following are errors in the above program
Main.java:4: error: incompatible types
         throw 10;
               ^
  required: Throwable
  found:    int
Main.java:6: error: unexpected type
      catch(int e) {
            ^
  required: class
  found:    int
2 errors
Question 2
class Test extends Exception { }
 
class Main {
   public static void main(String args[]) { 
      try {
         throw new Test();
      }
      catch(Test t) {
         System.out.println("Got the Test Exception");
      }
      finally {
         System.out.println("Inside finally block ");
      }
  }
}
A
Got the Test Exception
Inside finally block 
B
Got the Test Exception
C
Inside finally block 
D
Compiler Error
Exception Handling    
Discuss it


Question 2 Explanation: 
In Java, the finally is always executed after the try-catch block. This block can be used to do the common cleanup work. There is no such block in C++.
Question 3
Output of following Java program?
class Main {
   public static void main(String args[]) {
      int x = 0;
      int y = 10;
      int z = y/x;
  }
}
A
Compiler Error
B
Compiles and runs fine
C
Compiles fine but throws ArithmeticException exception
Exception Handling    
Discuss it


Question 3 Explanation: 
ArithmeticException is an unchecked exception, i.e., not checked by the compiler. So the program compiles fine. See following for more details. Checked vs Unchecked Exceptions in Java
Question 4
class Base extends Exception {}
class Derived extends Base  {}

public class Main {
  public static void main(String args[]) {
   // some other stuff
   try {
       // Some monitored code
       throw new Derived();
    }
    catch(Base b)     { 
       System.out.println("Caught base class exception"); 
    }
    catch(Derived d)  { 
       System.out.println("Caught derived class exception"); 
    }
  }
} 
A
Caught base class exception
B
Caught derived class exception
C
Compiler Error because derived is not throwable
D
Compiler Error because base class exception is caught before derived class
Exception Handling    
Discuss it


Question 4 Explanation: 
See Catching base and derived classes as exceptions Following is the error in below program
Main.java:12: error: exception Derived has already been caught
    catch(Derived d)  { System.out.println("Caught derived class exception"); } 
Question 5
class Test
{
    public static void main (String[] args)
    {
        try
        {
            int a = 0;
            System.out.println ("a = " + a + "\n");
            int b = 20 / a;
            System.out.println ("b = " + b);
        }

        catch(ArithmeticException e)
        {
            System.out.println ("Divide by zero error");
        }

        finally
        {
            System.out.println ("inside the finally block");
        }
    }
}

A
Compile error
B
Divide by zero error
C
a = 0
Divide by zero error
inside the finally block
D
a = 0
E
inside the finally block
Exception Handling    
Discuss it


Question 5 Explanation: 
On division of 20 by 0, divide by zero exception occurs and control goes inside the catch block. Also, the finally block is always executed whether an exception occurs or not.
Question 6
class Test
{
    public static void main(String[] args)
    {
        try
        {
            int a[]= {1, 2, 3, 4};
            for (int i = 1; i <= 4; i++)
            {
                System.out.println ("a[" + i + "]=" + a[i] + "\n");
            }
        }
        
        catch (Exception e)
        {
            System.out.println ("error = " + e);
        }
        
        catch (ArrayIndexOutOfBoundsException e)
        {
            System.out.println ("ArrayIndexOutOfBoundsException");
        }
    }
}
A
Compiler error
B
Run time error
C
ArrayIndexOutOfBoundsException
D
Error Code is printed
E
Array is printed
Exception Handling    
Discuss it


Question 6 Explanation: 
ArrayIndexOutOfBoundsException has been already caught by base class Exception. When a subclass exception is mentioned after base class exception, then error occurs.
Question 7
Predict the output of the following program.
class Test
{
    String str = "a";

    void A()
    {
        try
        {
            str +="b";
            B();
        }
        catch (Exception e)
        {
            str += "c";
        }
    }

    void B() throws Exception
    {
        try
        {
            str += "d";
            C();
        }
        catch(Exception e)
        {
            throw new Exception();
        }
        finally
        {
            str += "e";
        }

        str += "f";

    }
    
    void C() throws Exception
    {
        throw new Exception();
    }

    void display()
    {
        System.out.println(str);
    }

    public static void main(String[] args)
    {
        Test object = new Test();
        object.A();
        object.display();
    }

}
A
abdef
B
abdec
C
abdefc
Exception Handling    
Discuss it


Question 7 Explanation: 
'throw' keyword is used to explicitly throw an exception. finally block is always executed even when an exception occurs. Call to method C() throws an exception. Thus, control goes in catch block of method B() which again throws an exception. So, control goes in catch block of method A().
Question 8
Predict the output of the following program.
class Test
{   int count = 0;

    void A() throws Exception
    {
        try
        {
            count++;
            
            try
            {
                count++;

                try
                {
                    count++;
                    throw new Exception();

                }
                
                catch(Exception ex)
                {
                    count++;
                    throw new Exception();
                }
            }
            
            catch(Exception ex)
            {
                count++;
            }
        }
        
        catch(Exception ex)
        {
            count++;
        }

    }

    void display()
    {
        System.out.println(count);
    }

    public static void main(String[] args) throws Exception
    {
        Test obj = new Test();
        obj.A();
        obj.display();
    }
}
A
4
B
5
C
6
D
Compilation error
Exception Handling    
Discuss it


Question 8 Explanation: 
‘throw’ keyword is used to explicitly throw an exception. In third try block, exception is thrown. So, control goes in catch block. Again, in catch block exception is thrown. So, control goes in inner catch block.
There are 8 questions to complete.
1

Company Wise Coding Practice    Topic Wise Coding Practice