Last Updated: December 6, 2025
30 quizzes
Propagate a checked exception from a method that reads a file
public void readConfig() IOExceptionClick an option to fill the blank:
Create a new instance of a custom exception to signal invalid user input
throw InvalidUserInputException("Email is invalid");Click an option to fill the blank:
Close a resource automatically using try-with-resources
try (FileReader reader = FileReader("data.txt")) {Click an option to fill the blank:
Which class is the direct superclass of all exceptions that can be thrown or caught in Java?
What happens if an unchecked exception is not caught anywhere?
Which block is guaranteed to execute whether or not an exception is thrown?
Which statement about multiple catch blocks is correct?
Which keyword is used in a method signature to indicate that it may pass exceptions to the caller?
Which of these is a checked exception?
What is a primary benefit of using custom exceptions?
Which interface must a resource implement to be used in try-with-resources?
What does exception chaining help preserve?
In a try-with-resources statement, when is close() called on the resource?
Which statement about checked exceptions is true?
Which is the best reason to use a finally block when manually managing resources?
Order the steps to define, throw, and handle a custom checked exception
Drag and drop to reorder, or use the arrows.
Order the steps to use try-with-resources for reading a text file
Drag and drop to reorder, or use the arrows.
What is the output of this code involving an unchecked exception?
1public class Test {
2 public static void main(String[] args) {
3 try {
4 int x = 10 / 0;
5 System.out.println("OK");
6 } catch (ArithmeticException e) {
7 System.out.println("Error: " + e.getClass().getSimpleName());
8 }
9 }
10}What is the single line of output when using finally?
1public class Test {
2 public static void main(String[] args) {
3 try {
4 System.out.println("try");
5 if (true) throw new RuntimeException();
6 } catch (RuntimeException e) {
7 System.out.println("catch");
8 } finally {
9 System.out.println("finally");
10 }
11 }
12}What is the output when a method declares throws but does not throw?
1import java.io.IOException;
2public class Test {
3 public static void risky() throws IOException {
4 System.out.println("safe");
5 }
6 public static void main(String[] args) throws IOException {
7 risky();
8 }
9}What is the output when a custom exception is chained?
1public class Test {
2 public static void main(String[] args) {
3 try {
4 callService();
5 } catch (ServiceException e) {
6 System.out.println(e.getCause().getClass().getSimpleName());
7 }
8 }
9 static void callService() {
10 try {
11 throw new IllegalStateException("low level");
12 } catch (IllegalStateException e) {
13 throw new ServiceException("wrapper", e);
14 }
15 }
16}
17class ServiceException extends RuntimeException {
18 public ServiceException(String msg, Throwable cause) { super(msg, cause); }
19}What is the single line of output from this try-with-resources code?
1public class Test {
2 public static void main(String[] args) {
3 try (DemoResource r = new DemoResource()) {
4 System.out.println(r.message());
5 }
6 }
7}
8class DemoResource implements AutoCloseable {
9 public String message() { return "working"; }
10 public void close() { /* no output */ }
11}Find the bug in this code that handles checked exceptions
Click on the line(s) that contain the bug.
import java.io.BufferedReader;import java.io.FileReader;public class Loader { public String load(String path) { BufferedReader reader = new BufferedReader(new FileReader(path)); return reader.readLine(); }}Find the bug in this code using multiple catch blocks
Click on the line(s) that contain the bug.
public class Parser { public int parse(String input) { try { return Integer.parseInt(input); } catch (Exception e) { System.out.println("General error"); } catch (NumberFormatException e) { System.out.println("Invalid number"); } return 0; }}Match each exception-related keyword with its main purpose
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Match the exception type with how the compiler treats it
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Complete the code to define and use a custom checked exception when validating an order amount
class InvalidOrderAmountException extends Exception { public InvalidOrderAmountException(String message) { super(message); }}public class OrderService { public void placeOrder(double amount) ___ InvalidOrderAmountException { if (amount <= 0) { ___ InvalidOrderAmountException("Amount must be positive"); } System.out.println("Order placed: " + amount); }}Click an option to fill blank 1:
Complete the code to use try-with-resources and handle IOExceptions
import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;public class ReaderUtil { public String readFirstLine(String path) ___ IOException { try (BufferedReader br = ___ BufferedReader(new FileReader(path))) { return br.readLine(); } ___ (IOException e) { System.out.println("Failed: " + e.getMessage()); return null; } }}Click an option to fill blank 1:
Click the line that causes the NullPointerException when accessing user data
Click on the line to select.
public class UserProfile { public static void main(String[] args) { String name = null; String message = "User: " + name.toUpperCase(); System.out.println(message); }}Click the line that first throws the checked exception in this chaining example
Click on the line to select.
import java.io.IOException;public class Service { public static void main(String[] args) { try { new Service().process(); } catch (RuntimeException e) { System.out.println(e.getCause().getMessage()); } } void process() { try { loadConfig(); } catch (IOException e) { throw new RuntimeException("wrap", e); } } void loadConfig() throws IOException { throw new IOException("config missing"); }}