Java traps · Chapter 11 of 16

An exception lost in finally

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Money
  • 3 tasks
  • Java
  • In the all labs plan

The first 2 chapters of Java traps are free. This one comes with the all labs plan.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Repro.java: a rejected line that the log never sees

    Write reproduce() in Repro.java. Make a fake Upload.Link (an anonymous class) whose send throws new IOException("rejected line 2: total does not add up") for the second line, and whose close throws new IOException("connection reset"). Upload three lines over it with Upload.upload, catch the IOException that upload throws, and return its message.

    The check runs your reproduce() twice: beside the original Upload.java it must return "connection reset" (the rejection is lost), and beside a fixed Upload.java it must return "rejected line 2: total does not add up". So both of your fake's methods must fail, and you must return what upload really threw. No real waiting, no network and no reading input: reproduce() must finish within 3 seconds.

  2. Task 2

    Upload.java: the real reason an upload failed

    Fix upload(List<String> lines, Upload.Link link) in Upload.java. When send and close both fail, it throws the send's exception with the close's attached as suppressed (getSuppressed); when only send fails, it throws the send's exception; when only close fails, after every line was sent, it throws the close's exception (head office never committed the file); when nothing fails, it returns how many lines it sent. In every case it closes the link exactly once.

    Keep the Link interface, what upload returns and its log line (upload: sent <n> lines) as they are. The check passes its own fake links, with their own messages, that count how often they are closed.

  3. Task 3

    UploadTest.java: a test that would have caught it

    Write a JUnit test class, UploadTest.java, that would have caught this bug before the night shift's uploads. The check runs your file three times: beside the original Upload.java, where at least one test must fail; beside the fixed Upload.java, where every test must pass; and beside a wrong fix that catches and ignores the close's exception, where at least one test must fail.

    So test both sides with assertThrows and fake links: when send and close both fail, upload throws the send's exception; and when only close fails, after every line was sent, upload still throws. No real waiting: each test must finish within 3 seconds.

What you will learn

  • Repro.java: a rejected line that the log never sees
  • Upload.java: the real reason an upload failed
  • UploadTest.java: a test that would have caught it

More in Money

Build "An exception lost in finally" yourself

Tapri Counter runs on Java, and Kabir, its senior developer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Java traps are free. This one comes with the all labs plan.