Study and learn Interview MCQ Questions and Answers on Java Exception Handling with TRY, CATCH, FINALLY, THROW and THROWS. Try With Resources is also covered. 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 and Java Theory Notes on Try With Resource before reading these objective questions.
public class ExceptionTest8 { public static void main(String[] args) throws ArithmeticException { System.out.println("I am happy."); throw new ArithmeticException(); } }
ArithmeticException is an unchecked exception. So, the compiler does not stop with errors.
public class ExceptionTest9 { public static void main(String[] args) { System.out.println("I am going to forest."); throw new ClassNotFoundException(); } }
ClassNotFoundException is a checked exception in Java. So, the compiler throws error for not handling it using TRY CATCH FINALLY statements.
Unresolved compilation problem: Unhandled exception type ClassNotFoundException
public class ExceptionTest10 { public static void main(String[] args) throws Exception { System.out.println("Good Morning Cortana."); throw new NoSuchMethodException(); } }
Good Morning Cortana. Exception in thread "main" java.lang.NoSuchMethodException at Project1/package1.ExceptionTest10.main(ExceptionTest10.java:8)
NoSuchMethodException is a checked exception in Java. Here, you are telling the compiler that the MAIN method throws some exception. So, the program compiles and outputs. As the code is throwing a newly created exception, the execution stops here.
Yes. You can catch. But do not catch as these indicate some serious problems.
Yes. Throwable is the superclass of both Error and Exception classes in Java.
An Error is treated like an Unchecked exception in Java. So, the compiler does not show any errors for not-handling the errors properly.
Yes. TRY-with-resource block automatically closes the specified resource automatically.
try(var1 initialized; var2 initialized;) { }
try(var1; var2;) { }
try(var1 initialized, var2 initialized;) { }
try(var1 uninitialized, var2 uninitialized;) { }
You should always initialize variables used inside TRY-WITH-RESOURCE block in order to close those automatically.
The compiler inserts the necessary code inside the FINALLY block (whether explicitly written or not by the programmer) to close the resources.
import java.io.*; public class ExceptionTest12 { public static void main(String[] args) { try(BufferedWriter bw = new BufferedWriter(new FileWriter("abc.txt"));) { System.out.println("Inside Try With Resource block."); } catch(Exception e) { } } }
The resource variable "bw" referring BufferedWriter will be closed automatically. Otherwise, the memory will be filled and wasted.
Yes. Use the normal TRY, CATCH and FINALLY blocks. Keep the code that closes resources inside FINALLY block.
Yes. You can use the objects of classes that implement java.lang.Closeable or java.lang.AutoCloseable inside TRY-WITH-RESOURCE.
public class ExceptionTest14 { public static void main(String[] args) { try { int a=10/1; try {int b=20/1;} catch(Exception e1) { System.out.println("b=20"); } } catch(Exception e2) {System.out.println("a=10");} } }
a=10 b=20
b=20 a=10
As no exception is thrown, no output is produced.
try { try{} catch(Exception e1){} finally{} } catch(Exception e2) {}
try { try{} catch(Exception e1){} finally{} } catch(Exception e2) { try{} catch(Exception e3){} finally{} }
try { } catch(Exception e2) { } finally { try{} catch(Exception e3){} finally{} }
Nesting of exception handling blocks is allowed in any order. So, a try-catch-finally can be kept inside another TRY or CATCH or FINALLY block without errors.
A try-with-resource statement treats the variables like FINAL. So, reinitializing is not possible and the compiler throws error.
You can create a custom exception class simply by subclassing the class Exception. You can throw or catch this user-generated exception like any predefined exception.
public class ExceptionTest15 { void show(int a) throws MyException { System.out.println("Hello Custom Exception"); int b = a/0; } public static void main(String[] args) { ExceptionTest15 obj = new ExceptionTest15(); obj.show(5); System.out.println("Bye Custom Exception"); } } class MyException extends Exception { MyException(){ super();} MyException(String name){ super(name); } }
Hello Custom Exception Bye Custom Exception
Hello Custom Exception
Bye Custom Exception
You need to catch or throw the custom exception as it is treated like checked. So, you can not simply call the method throwing a checked exception. If it is non-checked, the compilation completes fine.