Skip to main content
Java Bug Hunt Hard +35 XP

Checked Exception Unhandled

This code won't compile.

JavaFind the bug
1public void readFile() {
2 FileReader f = new FileReader("data.txt");
3}
The bug is on line 2

Select the correct fix

Show answer and explanation

Answer: Add throws IOException or try-catch

FileReader throws a checked IOException. Java requires you to either handle it with try-catch or declare it with throws.

Hint: Checked exceptions must be declared or caught.

What this challenge tests

Bug Hunt in Java: exceptions, checked.

Related challenges