Exceptions - Quiz
Total: 8 questions
1. How to avoid execution of the finally block?
How to avoid execution of the finally block?
The finally block executes almost always, except when: System.exit() or Runtime.getRuntime().halt() is called (JVM terminates immediately), JVM crashes or is forcibly killed (e.g., kill -9), control never reaches finally (infinite loop or deadlock), or daemon threads are stopped during JVM shutdown.
2. What the try-with-resources block is used for?
What the try-with-resources block is used for?
It gives the possibility to declare resources in the try block so they can be closed without doing it explicitly in a finally block.
3. Which objects can be used in the try-with-resources block as a resource?
Which objects can be used in the try-with-resources block as a resource?
Any object that implements java.lang.AutoCloseable.
4. How does try-catch-finally work in Java?
How does try-catch-finally work in Java?
The try block contains code that may throw an exception. When an exception is thrown, the rest of the try block is skipped and the catch blocks are checked from top to bottom: only the first one whose type matches the exception (or is its superclass) runs, the others are skipped. The optional finally block runs after try and catch almost always — whether an exception was thrown or not, caught or not — which makes it the traditional place for cleanup. Then execution continues after the whole statement (an uncaught exception propagates to the caller after finally). Both checked and unchecked exceptions can be caught.
5. Can you use try without catch in Java?
Can you use try without catch in Java?
Yes, if there is a finally block: a try-finally statement is legal. It does not handle the exception — the exception propagates to the caller — but it guarantees that the cleanup code in finally runs first. Valid combinations are try-catch, try-finally, try-catch-finally and try-with-resources (Java 7+), which may have neither catch nor finally. A bare try { ... } without catch, finally or resources is a compile-time error.
6. What does a method return if both try and finally contain return?
What does a method return if both try and finally contain return?
The value from finally: try { return 1; } finally { return 2; } returns 2. If the try block threw an exception, a return in finally silently cancels its propagation, and the caller never learns about the problem. If finally only changes a variable, the result stays the same: int x = 1; try { return x; } finally { x = 10; } returns 1, because the return value is evaluated and stored before finally runs (changes to the contents of a returned mutable object, such as a list, are visible). The compiler allows return, break, continue and throw in finally, but such code is considered a design error.
7. In what order should multiple catch blocks be placed?
In what order should multiple catch blocks be placed?
Subclasses must come before their superclasses. A catch block catches the specified class and all of its subclasses, and only the first matching block runs, so catch (IOException e) placed after catch (Exception e) would be unreachable — a compile-time error: exception java.io.IOException has already been caught. Another compiler rule: a catch block for a checked exception (for example, IOException) that the try block cannot throw does not compile either; this rule does not apply to unchecked exceptions, Exception and Throwable.
8. What is multi-catch in Java and what are its restrictions?
What is multi-catch in Java and what are its restrictions?
Multi-catch (Java 7+) is a single catch block that handles several exception types separated by |: catch (IOException | SQLException e). It removes duplicated handlers. Restrictions: the alternatives must not be subclasses of each other (catch (FileNotFoundException | IOException e) does not compile), and the multi-catch parameter is implicitly final, so it cannot be reassigned. In a regular single-type catch block the parameter is not final and can be reassigned, unless it is declared final explicitly.