BackendBench
Coffee
Spot the Bug #38

The wrapped exception without its cause

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: 6

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

What is wrong

The new exception does not carry the original as its cause. The stack trace shows only the wrapper, so the real reason (missing file, permission, disk error) is lost, and the person debugging has to guess.

The fix

throw new IllegalStateException("import failed for " + path, e);

How to avoid it next time

Always pass the caught exception as the cause when wrapping, and add the details needed to reproduce the problem.

Want the background? Read Exception causes and stack traces 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โ† #37#39 โ†’

Frequently asked questions

What is the bug in "The wrapped exception without its cause"?

The new exception does not carry the original as its cause. The stack trace shows only the wrapper, so the real reason (missing file, permission, disk error) is lost, and the person debugging has to guess. The faulty line is line 6: throw new IllegalStateException("import failed");

How do I fix it?

Replace it with: throw new IllegalStateException("import failed for " + path, e);. Always pass the caught exception as the cause when wrapping, and add the details needed to reproduce the problem.

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