Core Java · Chapter 24 of 48
Exceptions and try-catch
By the end of this chapter you can catch what goes wrong, keep going past bad input, deal with checked exceptions, and make cleanup run whatever happens. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Core Java
- 5 tasks
- Java
- In the all labs plan
The first 2 chapters of Core Java are free. This one comes with the all labs plan.
Why you need this
At 11 pm the Mumbai CSMT counter closed its shift and Tapri Counter crashed on the last screen. Someone had typed "lots" instead of a number in the shift log, and the program died before it locked the cash drawer. Ravi found the drawer open the next morning. He wants bad lines skipped, and the drawer locked, always.
With what you know so far, one bad line ends the whole program: the exception rises out of every method and stops it, and the line that locks the drawer never runs. You could try to check every string by hand before parsing it, but you would miss a case, and some failures (a jammed drawer, an unreadable file) cannot be checked in advance at all.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
ShiftLog.java: read a cup count and survive text that is not a number
In ShiftLog.java, make parseCups(text) return the whole number in text, or 0 when text is not a whole number: words, an empty string, a decimal like 4.5, or null. A negative number is still a number. Catch only the exception Integer.parseInt throws, nothing broader.
For example, "12" returns 12, "-3" returns -3, and "twelve", "", "4.5" and null all return 0.
- Task 2
ShiftLog.java: add up the shift log, skipping broken lines with one multi-catch
In ShiftLog.java, make totalCups(lines) return the sum of the cups on every good line of the log. Each line is a station, a comma and a number of cups. A line is broken when it has no second part after a comma (NDLS, SBC followed by only a comma, or an empty line) or when that part is not a whole number (CSMT,lots): skip it and keep going. Spaces around the number are fine. Use one catch for both kinds of broken line. An empty log returns 0.
For example, SBC,12 then NDLS then CSMT,lots then MAS,5 returns 17; NDLS, 3 then SBC,12 with a space after it, then SBC, then an empty line returns 15.
- Task 3
ShiftLog.java: lock the cash drawer even when counting fails
In ShiftLog.java, make closeShift(drawer) count the drawer and return "Counted ₹" followed by the amount. The drawer must be locked afterwards, always: also when the drawer's count throws an IllegalStateException. Do not catch that exception: it must still reach the caller, after the drawer is locked.
For example, a drawer holding 4200 gives Counted ₹4200 and ends up locked; a jammed drawer throws its IllegalStateException with the message drawer jammed, and ends up locked too.
- Task 4
ShiftLog.java: handle the checked IOException from a reader
In ShiftLog.java, make firstLine(log) return the first line of the log, an empty string "" when the log is empty, or "unreadable" when reading throws an IOException.
For example, a log holding SBC,12 then NDLS,3 gives SBC,12; an empty log gives ""; a log whose reader throws an IOException gives unreadable.
- Task 5 · interview question
Interview: ShiftLog.java, predict what try, catch and finally return
In ShiftLog.java, make predictOutput() return the lines the code in its comment prints, one string per line, in order, written exactly as Java prints them.
The check runs the same code and compares your list with what Java really printed.
What you will learn
- try and catch: stop an exception and handle it where you can.
- Multi-catch: one catch for several exception types.
- Checked and unchecked: which exceptions the compiler makes you handle.
- finally: code that runs however the try ends.
More in Core Java
Build "Exceptions and try-catch" yourself
Tapri runs chai counters at railway stations, and every counter runs a Java program you write. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Core Java are free. This one comes with the all labs plan.