AlgoMaster Logo

Exception Handling - Quiz

Last Updated: December 6, 2025

Exception Handling Exercises

30 quizzes

1
Code Completion

Propagate a checked exception from a method that reads a file

java
1
public void readConfig() IOException

Click an option to fill the blank:

2
Code Completion

Create a new instance of a custom exception to signal invalid user input

java
1
throw InvalidUserInputException("Email is invalid");

Click an option to fill the blank:

3
Code Completion

Close a resource automatically using try-with-resources

java
1
try (FileReader reader = FileReader("data.txt")) {

Click an option to fill the blank:

4
Multiple Choice

Which class is the direct superclass of all exceptions that can be thrown or caught in Java?

5
Multiple Choice

What happens if an unchecked exception is not caught anywhere?

6
Multiple Choice

Which block is guaranteed to execute whether or not an exception is thrown?

7
Multiple Choice

Which statement about multiple catch blocks is correct?

8
Multiple Choice

Which keyword is used in a method signature to indicate that it may pass exceptions to the caller?

9
Multiple Choice

Which of these is a checked exception?

10
Multiple Choice

What is a primary benefit of using custom exceptions?

11
Multiple Choice

Which interface must a resource implement to be used in try-with-resources?

12
Multiple Choice

What does exception chaining help preserve?

13
Multiple Choice

In a try-with-resources statement, when is close() called on the resource?

14
Multiple Choice

Which statement about checked exceptions is true?

15
Multiple Choice

Which is the best reason to use a finally block when manually managing resources?

16
Sequencing

Order the steps to define, throw, and handle a custom checked exception

Drag and drop to reorder, or use the arrows.

17
Sequencing

Order the steps to use try-with-resources for reading a text file

Drag and drop to reorder, or use the arrows.

18
Output Prediction

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}
19
Output Prediction

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}
20
Output Prediction

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}
21
Output Prediction

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}
22
Output Prediction

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}
23
Bug Spotting

Find the bug in this code that handles checked exceptions

Click on the line(s) that contain the bug.

java
1
import java.io.BufferedReader;
2
import java.io.FileReader;
3
public class Loader {
4
    public String load(String path) {
5
        BufferedReader reader = new BufferedReader(new FileReader(path));
6
        return reader.readLine();
7
    }
8
}
24
Bug Spotting

Find the bug in this code using multiple catch blocks

Click on the line(s) that contain the bug.

java
1
public class Parser {
2
    public int parse(String input) {
3
        try {
4
            return Integer.parseInt(input);
5
        } catch (Exception e) {
6
            System.out.println("General error");
7
        } catch (NumberFormatException e) {
8
            System.out.println("Invalid number");
9
        }
10
        return 0;
11
    }
12
}
25
Matching

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.

26
Matching

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.

27
Fill in the Blanks

Complete the code to define and use a custom checked exception when validating an order amount

java
1
class InvalidOrderAmountException extends Exception {
2
public InvalidOrderAmountException(String message) {
3
super(message);
4
}
5
}
6
public class OrderService {
7
public void placeOrder(double amount) ___ InvalidOrderAmountException {
8
if (amount <= 0) {
9
___ InvalidOrderAmountException("Amount must be positive");
10
}
11
System.out.println("Order placed: " + amount);
12
}
13
}

Click an option to fill blank 1:

28
Fill in the Blanks

Complete the code to use try-with-resources and handle IOExceptions

java
1
import java.io.BufferedReader;
2
import java.io.FileReader;
3
import java.io.IOException;
4
public class ReaderUtil {
5
public String readFirstLine(String path) ___ IOException {
6
try (BufferedReader br = ___ BufferedReader(new FileReader(path))) {
7
return br.readLine();
8
} ___ (IOException e) {
9
System.out.println("Failed: " + e.getMessage());
10
return null;
11
}
12
}
13
}

Click an option to fill blank 1:

29
Hotspot Selection

Click the line that causes the NullPointerException when accessing user data

Click on the line to select.

java
1
public class UserProfile {
2
    public static void main(String[] args) {
3
        String name = null;
4
        String message = "User: " + name.toUpperCase();
5
        System.out.println(message);
6
    }
7
}
30
Hotspot Selection

Click the line that first throws the checked exception in this chaining example

Click on the line to select.

java
1
import java.io.IOException;
2
public class Service {
3
    public static void main(String[] args) {
4
        try {
5
            new Service().process();
6
        } catch (RuntimeException e) {
7
            System.out.println(e.getCause().getMessage());
8
        }
9
    }
10
    void process() {
11
        try {
12
            loadConfig();
13
        } catch (IOException e) {
14
            throw new RuntimeException("wrap", e);
15
        }
16
    }
17
    void loadConfig() throws IOException {
18
        throw new IOException("config missing");
19
    }
20
}

Premium Content

Subscribe to unlock full access to this content and more premium articles.