Exceptions: Java 25 practice questions

Exceptions is 8% of the free Java 25 mock (4 of 50 questions). Pick an answer to see right away whether it is correct, with the explanation and the documentation section it comes from.

J.V · Java 25 practice question

What happens when a developer attempts to compile this Java 25 program?

public class Main {
  static class Res implements AutoCloseable {
    public void close() {}
  }
  public static void main(String[] args) {
    Res r = new Res();
    r = new Res();
    try (r) {
      System.out.println("used");
    }
  }
}
Choose one answer
Check answer and explanation

Correct answer: It fails to compile because r is neither final nor effectively final

Explanation

An existing variable referenced in a try-with-resources resource specification must be final or effectively final and definitely assigned before the try statement; reassigning r violates effective finality and causes a compile-time error.

Common trap

Even though r is reassigned before the try statement begins, any reassignment prevents it from being effectively final. The compiler rejects the non-effectively-final resource variable rather than tracking multiple instances.

Source: JLS SE 25 §14.20.3. try -with-resources
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.V · Java 25 practice question

What happens when a developer compiles this Java 25 program?

void main() {
  try {
    throw new IllegalArgumentException();
  } catch (IllegalArgumentException | IllegalStateException e) {
    e = new RuntimeException();
  }
}
Choose one answer
Check answer and explanation

Correct answer: It fails to compile because the multi-catch exception parameter e is implicitly final

Explanation

An exception parameter declared in a multi-catch clause is implicitly final even without the final modifier, so reassigning e inside the catch block causes a compile-time error.

Common trap

While a single-type catch parameter can be reassigned if not marked final, a multi-catch parameter is always implicitly final. Adding RuntimeException would not make e assignable and would also violate the multi-catch subtype rule.

Source: JLS SE 25 §14.20. The try statement
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.V · Java 25 practice question

What happens when this Java 25 program runs?

void main() {
  try {
    int x = 10;
  } finally {
    throw new IllegalStateException("cleanup");
  }
}
Choose one answer
Check answer and explanation

Correct answer: The try statement completes abruptly by throwing IllegalStateException

Explanation

When the try block completes normally, the finally block executes; because the finally block completes abruptly by throwing IllegalStateException, the entire try statement completes abruptly for that same reason.

Common trap

Normal completion of the try block does not override an abrupt completion in the finally block. A finally block is legally allowed to throw exceptions.

Source: JLS SE 25 §14.20.2. Execution of try - finally and try - catch - finally
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.V · Java 25 practice question

A developer is inspecting the Java exception hierarchy to build a custom error-reporting utility. Which class is the direct superclass of Throwable?

Choose one answer
Check answer and explanation

Correct answer: Object

Explanation

In Java, Throwable directly extends Object, and Throwable together with all of its subclasses form the exception classes.

Common trap

Exception is a direct subclass of Throwable, not its superclass. RuntimeException is a subclass of Exception, sitting two levels below Throwable.

Source: JLS SE 25 §11.1.1. The Kinds of Exceptions
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.V · Java 25 practice question

An optimizing JVM speculatively executes expressions that appear after a statement which ends up throwing an exception at runtime. What does the Java Language Specification require regarding those speculatively executed expressions?

Choose one answer
Check answer and explanation

Correct answer: The optimized code must hide the speculative execution from the user-visible state of the program

Explanation

If optimized code speculatively executes expressions or statements following the point where an exception occurs, it must hide that speculative execution from the program's user-visible state.

Common trap

Speculative effects must never leak into user-visible state, whether or not the exception is caught locally. Speculative execution is permitted as long as its effects are hidden when an earlier exception occurs.

Source: JLS SE 25 §11.3. Run-Time Handling of an Exception
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.V · Java 25 practice question

What is the result of compiling this Java 25 program?

public class Main {
  static class Res implements AutoCloseable {
    public void close() {}
  }
  public static void main(String[] args) {
    try (var r = new Res()) {
      r = null;
    }
  }
}
Choose one answer
Check answer and explanation

Correct answer: It fails to compile because a local variable declared in a resource specification is implicitly final

Explanation

A local variable declared in a try-with-resources resource specification is implicitly final if not explicitly declared final, so reassigning r to null causes a compile-time error.

Common trap

Reassigning a resource variable is disallowed at compile time precisely so the resource reference cannot be changed before closing. The code never reaches runtime because the compiler rejects the assignment to an implicitly final variable.

Source: JLS SE 25 §14.20.3. try -with-resources
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.V · Java 25 practice question

A developer wants to define a custom exception type representing a recoverable failure in a file-parsing service. Which direct subclass of Throwable is designated as the superclass for exceptions from which ordinary programs may wish to recover?

Choose one answer
Check answer and explanation

Correct answer: Exception

Explanation

Exception is the direct subclass of Throwable that serves as the superclass for all exceptions from which ordinary programs may want to recover.

Common trap

Error is a direct subclass of Throwable, but it is reserved for abnormal conditions that ordinary programs are not expected to recover from. Object is the superclass of Throwable, not a subclass of Throwable.

Source: JLS SE 25 §11.1.1. The Kinds of Exceptions
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.V · Java 25 practice question

A worker thread throws an exception that no catch clause on its call stack can handle. The thread has an uncaught exception handler configured and an active finally block on the stack. What happens before the thread terminates?

Choose one answer
Check answer and explanation

Correct answer: All active finally clauses are executed, and the thread's uncaught exception handler is executed

Explanation

When no catch clause can handle an exception, the current thread is terminated, and before termination all finally clauses are executed and the thread's uncaught exception handler is executed if set.

Common trap

Finally blocks are not skipped during uncaught exception unwinding; they execute before thread termination. Finally blocks always run as the stack unwinds, even when no catch block handles the exception.

Source: JLS SE 25 §11.3. Run-Time Handling of an Exception
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.V · Java 25 practice question

A developer writes a multi-catch clause: catch (IllegalArgumentException | IllegalStateException e). According to the JLS, what is the declared type of the exception parameter e?

Choose one answer
Check answer and explanation

Correct answer: The least upper bound lub(IllegalArgumentException, IllegalStateException)

Explanation

For an exception parameter whose type is a union D1 | D2 | ... | Dn, the declared type is the least upper bound lub(D1, D2, ..., Dn), which here is RuntimeException.

Common trap

The declared type is not hardcoded to Exception; it is computed as the least upper bound of the specific alternatives. The parameter is typed to the least upper bound of the exception classes, not Object, so Throwable methods like getMessage() are directly accessible.

Source: JLS SE 25 §14.20. The try statement
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


Back to all topics