Java OCA OCP Practice Question 705

Question

Consider the following code snippet:

void m1 () throws Exception{ 
   try{ // www. j av  a2  s.  c  om
      // line1 
    } 
   catch  (IOException e){ 
       throw new SQLException (); 
    } 
   catch (SQLException e){ 
       throw new InstantiationException (); 
    } 
   finally{ 
      throw new CloneNotSupportedException ();   // this is not a RuntimeExc 
    } 
} 

Which of the following statements are true?

Select 2 options

  • A. If IOException gets thrown at line 1, then the whole method will end up throwing SQLException.
  • B. If IOException gets thrown at line 1, then the whole method will end up throwing CloneNotSupportedException.
  • C. If IOException gets thrown at line 1, then the whole method will end up throwing InstantiationException()
  • D. If no exception is thrown at line 1, then the whole method will end up throwing CloneNotSupportedException.
  • E. If SQLException gets thrown at line 1, then the whole method will end up throwing InstantiationException()


Correct Options are  : B D

Note

The Exception that is thrown in the last, is the Exception that is thrown by the method to the caller.

When no exception or any exception is thrown at line 1, the control goes to finally or some catch block.

Even if the catch blocks throws some exception, the control goes to finally.

The finally block throws CloneNotSupportedException, so the method ends up throwing CloneNotSupportedException.

Other exceptions thrown by the code prior to this point are lost.

Exception thrown by a catch cannot be caught by the following catch blocks at the same level.

If IOException is thrown at line 1, the control goes to first catch which throws SQLException.

Now, although there is a catch for SQLException, it won't catch the exception because it is at the same level.

The control goes to the finally and same process as explained above continues.

Any exceptions thrown before this exception are lost.




PreviousNext

Related