Java - Interview Questions and Answers on Try - Catch

Q1.  How does a try statement determine which catch clause should be used to handle an exception?

Ans. When an exception is thrown within the body of a try statement, the catch clauses of the try statement are examined in the order in which they appear. The first catch clause that is capable of handling the exception is executed. The remaining catch clauses are ignored.

Q2.  Is it necessary that each try block to be followed by catch block ? 

Ans. It should be followed by either catch or finally block.

Q3.  Can finally block be used without catch ?

Ans. Yes but should follow "try" block then.

Q4.  How finally used under Exception Handling?

Ans. The finally keyword is used to create a block of code that follows a try block. A finally block of code always executes, whether or not an exception has occurred.

Q5.  Can try statements be nested?

Ans. Yes

Q6.  Will finally be called always if all code has been kept in try block ?

Ans. The only time finally won't be called is if you call System.exit() or if the JVM crashes first.

Q7.  Can we have try and catch blocks within finally ?

Ans. Yes