Unit 2 home
Class 18 of 60 Part H · Slate UI24PC320CS
Placeholder · not written yet

Catch the error, release the resource, always

A movie-ticket counter holds a seat the moment you start booking. Whether the booking succeeds or explodes, that seat has to be released — and that is exactly the shape finally was invented for.

Class 18 Part H · C17–C20 13 pages planned 1 PYQ · P1·Q3

This session has not been authored yet

You have reached a real page, but an empty one. The deck for Class 18 is planned in full in the course knowledge document and is queued for authoring — nothing on this page is finished teaching content.

The roster underneath is the actual plan for this session, so you can see exactly what will land here.

What this class will cover

  1. Class-18 openerThe hook: catch the error, release the resource, always.
  2. Basic try / catchThe syntax, and the exact path execution takes when something throws.
  3. finally — always runsThe cleanup block, and the handful of genuine exceptions to "always".
  4. Try-with-resourcesJava 7+, AutoCloseable, and why it is cleaner than a hand-written finally.
  5. Multiple catch blocksOrder matters — specific before general, or the compiler stops you.
  6. PYQ · P1·Q3 · 2 marksMultiple catch blocks for a single try, with an example.
  7. Self-study · multi-catch in one blockJava 7's catch (A | B e) — a convenience over two separate blocks.
  8. throw vs throwsThe statement versus the declaration — the confusion the exam relies on.
  9. Worked example · movie-ticket bookingA counter catching NumberFormatException and ArithmeticException, with the seat hold released in finally on every path.
  10. Activity 1 · debugging catch orderA general catch(Exception e) sitting before a specific one — find why the compiler refuses it.
  11. Activity 2 · fill in the finally blockWrite the two lines that release the seat and close the Scanner, then the Class-18 close card.

Why a movie ticket, and not a payment

try / catch / finally only earns its keep when a resource must be released on both the success and the failure path. A held seat is exactly that shape — and it also seeds the MovieTicketCounter domain that Class 23 and Lab 5 will race threads against.