BackendBench
Coffee
Spot the Bug #40

Logged, but the code carries on

A medium Java puzzle in the exception handling mistakes family. One line below contains a real bug. Try to spot it first, then read why it fails and how to fix it.

mediumException handling mistakesbuggy line: 12

Highlighted: line 12. Play this one as a puzzle without the answer.

What is wrong

The catch block only prints a message, then the method continues as if loading had succeeded. config is still null, so the next line throws a NullPointerException whose stack trace points at the wrong place and hides the real cause.

The fix

catch (Exception e) { throw new IllegalStateException("could not load config", e); }

How to avoid it next time

After catching an exception, either recover completely or stop. Do not continue with values that were never set.

Want the background? Read Exceptions: try, catch, throw in the Java concepts library.

About exception handling mistakes

Swallowed exceptions, lost causes and returns in finally blocks hide failures until the data is already wrong. The program keeps running and nobody knows why the result is missing.

In code review: An empty catch block, a catch that only prints, or an exception rethrown without its cause are all red flags. Log with the exception object and rethrow or handle meaningfully.

More puzzles in this family

๐Ÿž Play today's puzzleAll 132 bugsโ† #39#41 โ†’

Frequently asked questions

What is the bug in "Logged, but the code carries on"?

The catch block only prints a message, then the method continues as if loading had succeeded. config is still null, so the next line throws a NullPointerException whose stack trace points at the wrong place and hides the real cause. The faulty line is line 12: return config.url();

How do I fix it?

Replace it with: catch (Exception e) { throw new IllegalStateException("could not load config", e); }. After catching an exception, either recover completely or stop. Do not continue with values that were never set.

How do I catch exception handling mistakes in code review?

An empty catch block, a catch that only prints, or an exception rethrown without its cause are all red flags. Log with the exception object and rethrow or handle meaningfully.

Is there a new puzzle every day?

Yes. The daily Spot the Bug puzzle picks one of these snippets for each UTC day, with the difficulty set by the weekday. Your streak is stored only in your browser.

More tools you might need