Study and learn Interview MCQ Questions and Answers on Java Exception Handling with TRY, CATCH and FINALLY blocks. Attend job interviews easily with these Multiple Choice Questions. You can print these Questions in default mode to conduct exams directly. You can download these MCQs in PDF format by Choosing Print Option first and Save as PDF option next using any Web Browser.
Go through Java Theory Notes on Exception Handling before reading these objective questions.
Yes. Simply surround all the code within TRY and CATCH.
No. An error can not be handled smooth without breaking the flow of execution.
The 5 exception handling keywords are try, catch, finally, throw and throws.
try{ //Java code with expected exceptions }
CATCH block follows TRY block. Based on the type of Exception raised or thrown, you can write some fall back code inside CATCH block.
public class ExceptionTest1 { public static void main(String[] args) { try { int a=9, b=0; int c = a/b; System.out.println("Exception occurred."); } catch(Exception e) { System.out.println("Catching an Exception."); } } }
Exception occurred. Catching an Exception.
Exception occurred.
Catching an Exception.
The first PRINTF with "Exception occurred." is not printed as the program flow already broke because of exception caused by division by zero.
Yes. Exceptions may or may not occur. It is the Java Runtime that monitors and creates an Exception object that will be handed over to the suitable CATCH block.
Yes. You can add as many CATCH blocks as you want with each Block catching one unique Exception type object.
In practice, exceptions are unexpected bugs which spoil the user experience.
public class ExceptionTest2 { public static void main(String[] args) { try { int ary[] = {10, 20, 30}; int tempt = ary[4]; } catch(ArrayIndexOutOfBoundsException e1) { System.out.println(e1.getMessage()); } catch(Exception e2) { System.out.println("Some exception"); } } }
Index 4 out of bounds for length 3
Index 4 out of bounds for length Some exception
Some exception
IndexOutOfBoundsException is raised by TRY block. Observe the order of catching the exceptions. Always catch a Subclass exception before a Superclass exception.
try { int num = 10/0; } catch(Exception e1) { System.out.println("EXCEPTION"); } catch(ArithmeticException e2) { System.out.println("ARITHMETIC EXCEPTION"); }
try { int num = 10/0; } catch(ArithmeticException e2) { System.out.println("ARITHMETIC EXCEPTION"); } catch(Exception e1) { System.out.println("EXCEPTION"); }
An exception of Subclass type should be caught before an exception of Superclass type. The ArithmeticException is a subclass of superclass Exception. So, you should first catch the ArithmeticException. Otherwise, all the next CATCH blocks will become unreachable and compiler throws error.
A FINALLY block in Java contains the code that will be executed whether an exception occurs or not. It usually contains the code to close the resources like files and devices. So, this FINALLY block is fail-safe.
public class ExceptionTest4 { public static void main(String[] args) { try { } catch(Exception e) { } finally { System.out.println("FINALLY block executed"); } } }
Even the TRY block contains no code to execute, the finally block will be called.
public class ExceptionTest5 { public static void main(String[] args) { int ary[] = new int[2]; ary[10] = 5; try { int number= 2/0; } catch(Exception e) { System.out.println("Divide by Zero"); } finally { System.out.println("Inside FINALLY block"); } } }
Divide by Zero Inside FINALLY block
As the exception (ArrayIndexOutOfBoundsException) occurs even before the TRY block, program execution halts and all the remaining code including FINALLY block will not be executed. Even if a single line of TRY block code is executed, its corresponding FINALLY block will be executed.
public class ExceptionTest6 { static void show() { try { System.out.println("inside TRY"); return; } finally { System.out.println("inside FINALLY"); } } public static void main(String[] args) { show(); } }
inside TRY
inside TRY inside FINALLY
inside FINALLY
Even if a RETURN statement is present at the last line of TRY block, the control is not returned to the calling method. The JVM searches for the suitable FINALLY block and executes it before returning. So, the FINALLY block has higher priority than a RETURN statement.
try{} catch{} finally{}
try{} finally{}
try{} catch{}
Yes. To write a FINALLY block, there should be a TRY block always.
Yes. There is no value to the CATCH block without a TRY block. The Java compiler throws error if you write CATCH blocks without a TRY.
There can be only one TRY block present in try-catch-finally series of blocks.
Yes. You can write as many CATCH blocks as you want without duplicating.
Yes. Unchecked Java exceptions will not be checked by the compiler for mishandling or throwing.
Yes. Checked exceptions must be handled properly.
These unchecked exceptions are subclasses of RuntimeException class.
These checked exceptions must always accompany with TRY-CATCH blocks and THROW-THROWS statements.