Java OCA OCP Practice Question 837

Question

What will be the result of compiling and running the following program ?

class Exception1 extends Exception  {} 
class Exception2 extends Exception  {} 
public class ExceptionTest{ 
   public static void main (String  [] args) throws Exception{ 
      try{ /*from   ww  w. j  a v  a  2s  .c o m*/
         m2 (); 
       } 
      finally{ m3 ();  } 
     } 
    public static void m2 () throws Exception1{  throw new Exception1 (); 
    public static void m3 () throws Exception2{  throw new AnotherExce 
} 

Select 1 option

  • A. It will compile but will throw Exception2 when run.
  • B. It will compile but will throw Exception1 when run.
  • C. It will compile and run without throwing any exceptions.
  • D. It will not compile.
  • E. None of the above.


Correct Option is  : A

Note

m2 () throws Exception1, which is not caught anywhere.

But before exiting out of the main method, finally must be executed.

Since finally throw Exception2, the Exception1 thrown in the try block is ignored and Exception2 is thrown from the main method.




PreviousNext

Related