Object-Oriented Programming through Java · Packages, Exceptions and I/O
Exceptions
In Object-Oriented Programming through Java because a sold-out hall is not a return code — throw, throws, try, catch, finally, and the checked versus unchecked split.
When the hall is full, the desk does not return -1 and hope. It throws an exception: an object that names what went wrong and unwinds the call until a catch handles it. throws warns callers. finally runs on the way out. Some exceptions the compiler forces you to name; some it does not.
- Object-Oriented Programming through Java
- Easy level
- 5 concepts
1An exception is an object that names a failure
An exception is an object thrown when a method cannot keep its promise. SoldOutException is not a printed seat — it is the fact that there is no seat.
The call stack unwinds until a catch for that type runs. If none does, the program stops and prints a trace.
Figure. take cannot give a seat, so it throws. book must catch or declare.
How it works
- An exception is an object that names a failureAn exception is a failure object. It is not a magic return value of -1.
2throw starts it; throws warns the caller
throw new SoldOutException("hall full"); creates the object and starts the unwind.
throws SoldOutException on the method tells the compiler — and the next author — that this method may pass that failure out instead of handling it.
Figure. throws SoldOutException sits on take and warns the caller. throw new SoldOutException("hall full") is the statement in the body that starts the unwind.
How it works
- throw starts it; throws warns the callerthrow is the statement. throws is the method's warning.
throw in the body; throws on the method
void take(String seat) throws SoldOutException {
if (full) {
throw new SoldOutException("hall full");
}
}3try marks the risk; catch handles a type
try { ... } catch (SoldOutException e) { ... } runs the try block and, if that type is thrown, runs the matching catch instead of crashing.
A catch is chosen by type. A parent type catch can swallow children. Order more specific types first.
Figure. try marks hall.take. If SoldOutException is thrown, the matching catch runs instead of a crash. A parent-type catch can swallow children, so more specific types come first.
Handle sold-out; let other failures travel
try {
hall.take("A12");
} catch (SoldOutException e) {
System.out.println(e.getMessage());
}Sold-out is caught, not printed as a seat
take throws IllegalArgumentException("hall full") when full is true. The catch prints the message. What does the program print?
- fulltrue
- throwIllegalArgumentException
- matching catch runsprints hall full
Pro tip. Without the catch, the program would stop. The catch is what turns the failure into a desk message.
Coding lab. Catch a sold-out hall runs in the app, with checks on your output.
4finally runs on the way out
finally { ... } runs after try, whether the try finished, threw, or returned. It is the place to close a file or a connection the try opened.
It is not a second catch. It does not handle the exception. It always gets a turn before the method leaves.
Figure. desk.unlock() in finally runs after try, whether take finished or SoldOutException was caught. finally does not handle the exception; it always gets a turn before the method leaves.
How it works
- finally runs on the way outfinally always runs. It is cleanup, not handling.
unlock runs after success or sold-out
try {
hall.take("A12");
} catch (SoldOutException e) {
System.out.println("full");
} finally {
desk.unlock();
}5Checked is forced; unchecked is not
A checked exception extends Exception but not RuntimeException. The compiler requires you to catch it or declare throws. IOException is the usual example.
An unchecked exception is a RuntimeException (or Error). NullPointerException and IllegalArgumentException are unchecked. You may catch them; the compiler will not force you.
No diagram — the idea is carried by the prose or the Java listing.
| Kind | Parent | Compiler |
|---|---|---|
| checked | Exception, not RuntimeException | must catch or throws |
| unchecked | RuntimeException or Error | optional |
Notes
- In Object-Oriented Programming through Java because a sold-out hall is not a return code — throw, throws, try, catch, finally, and the checked versus unchecked split.
- throw creates the event. throws declares it on a method. try marks the risky block; catch handles a type; finally always runs.
- Checked exceptions extend Exception but not RuntimeException. Unchecked ones are RuntimeException and Error.
Exam traps & shortcuts
- catch (Exception e) hides the type you should have handled. Catch the sold-out type if that is what you mean.
- return inside try still runs finally before the method leaves.
Recap
throw starts a failure object. throws warns. try/catch handles a type. finally cleans up. Checked is compulsory.
- throw vs throws
- throw in the body. throws on the method.
- catch by type
- More specific catch first. finally is not a catch.
- checked
- Exception minus RuntimeException. IOException is the type you will meet in files.
Practise Exceptions
Reading is free and needs no account. Practice, mocks and progress live in the app.
- Timed mocks scored with the real marking scheme
- Readiness tracked per topic, kept on your device