Java Exception Handling

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);
      }
  }
}
Cross
Got the Exception 10
Cross
Got the Exception 0
Tick
Compiler Error


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 ");
      }
  }
}
Tick
Got the Test Exception
Inside finally block 
Cross
Got the Test Exception
Cross
Inside finally block 
Cross
Compiler Error


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;
  }
}
Cross
Compiler Error
Cross
Compiles and runs fine
Tick
Compiles fine but throws ArithmeticException exception


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"); 
    }
  }
} 
Cross
Caught base class exception
Cross
Caught derived class exception
Cross
Compiler Error because derived is not throwable
Tick
Compiler Error because base class exception is caught before derived class


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

Cross
Compile error
Cross
Divide by zero error
Tick
a = 0
Divide by zero error
inside the finally block
Cross
a = 0
Cross
inside the finally block


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");
        }
    }
}
Tick
Compiler error
Cross
Run time error
Cross
ArrayIndexOutOfBoundsException
Cross
Error Code is printed
Cross
Array is printed


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

}
Cross
abdef
Tick
abdec
Cross
abdefc


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();
    }
}
Cross
4
Tick
5
Cross
6
Cross
Compilation error


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.
Question 9
Which of these is a super class of all errors and exceptions in the Java language?
Cross
RunTimeExceptions
Tick
Throwable
Cross
Catchable
Cross
None of the above


Question 9-Explanation: 
All the errors and exception types are subclasses of the built in class Throwable in the java language. Throwable class is the superclass of all errors and exceptions in the Java language. Option (B) is correct.
Question 10
The built-in base class in Java, which is used to handle all exceptions is
Cross
Raise
Cross
Exception
Cross
Error
Tick
Throwable


Question 10-Explanation: 
Throwable class is the built-in base class used to handle all the exceptions in Java.
There are 10 questions to complete.


  • Last Updated : 27 Sep, 2023

Share your thoughts in the comments
Similar Reads