The concept of a software exception was introduced into programming in the early 60s. Since then, it has been adopted by many languages and has evolved significantly.
It is generally accepted that exceptions help separate different concerns in programming and therefore improve manageability of code. While very few people will deny the usefulness of exceptions, there is ongoing debate on how they should be used.
This book covers the basics of Java exceptions, advanced concept, and best practices, and gives a historical overview of how exception handling in Java was and is approached. In addition, this book provides examples of real APIs with analysis on how these APIs approach and handle exceptions.
This book assumes that the reader is familiar with the basics of the Java programming language, and is able to write, compile and execute a simple program. It also assumes that the reader is able to read and understand code excerpts.
We hope you both enjoy and learn from this book.
AI Reading Assistant
Whole-book reading guide from stratified index samples; jump to passages in the text
Tip the Site
Support this siteYour recognition and a small knowledge-service contribution help keep this technical work open source.Scan the WeChat Pay or Alipay code below. Logged-in and guest visitors can both tip.
WeChat Pay
Alipay
Open WeChat or Alipay and scan. No login required.
AI guide
【One-Line Pitch】
A focused, opinionated guide to Java exception handling that moves from the mechanics of `try`/`catch`/`finally` and try-with-resources to the long-running debate over checked exceptions, illustrated with how real APIs (JDK, Hibernate, Spring, Hadoop, AWS) actually behave. Best for intermediate Java developers who already write code but want to make deliberate, defensible decisions about error handling.
【Book Arc】
- **Opening (~0%–9%)**: Sets the stage with a historical sweep — interrupts, early languages (Lisp, Ada, COBOL), C/C++, and how exceptions merged with OOP semantics — establishing why exceptions exist and why their usage remains contested.
- **Early (~9%–33%)**: The essentials: the `Throwable` hierarchy, the checked/unchecked split and its original intent, how exceptions are created, thrown, re-thrown, and detected by the JVM, plus the core `try`/`catch`/`finally` blocks and multi-catch.
- **Middle (~33%–55%)**: The mechanics get subtle — try-with-resources, `AutoCloseable`, resource closing order, suppressed exceptions, and the tricky transfer-of-control cases (`break`, `continue`, `return` interacting with `finally` and resource cleanup).
- **Late (~55%–75%)**: Patterns and anti-patterns (the central pattern, not using exceptions for flow control, avoiding overly broad catches, preserving causes, logging vs. re-throwing) followed by the two major views on checked exceptions, with named expert positions.
- **Ending (~75%–100%)**: Real API case studies (Java SE, JPA, Hibernate, Spring 1.x–5.x, Hadoop 1.x–3.x, Amazon WS) and modern challenges around non-"true path" cases and growing complexity.
【Key Takeaways】
- **Exceptions are objects, and that matters** (Early): Because Java represents exceptions as classes rooted at `Throwable`, you can encapsulate context, add fields, and separate error information from normal flow — the core reason OOP exceptions beat raw error codes.
- **The checked/unchecked distinction is a design contract, not a technical necessity** (Early): Java enforces handling of "recoverable abnormal" conditions at compile time; the book frames this as a deliberate, and heavily criticized, language decision rather than an obvious good.
- **`try`/`catch`/`finally` has non-obvious control-flow rules** (Middle): Catch blocks for child types can't follow parent types, multi-catch forbids parent-child pairs, and `finally` can override transfers of control — details that cause real bugs.
- **try-with-resources changes cleanup semantics** (Middle): Resources close in reverse initialization order, and exceptions thrown during `close()` become *suppressed* exceptions attached to the original — a behavior you must understand to debug cleanup failures.
- **Anti-patterns are the practical heart of the book** (Late): Swallowing exceptions, overly broad `catch`/`throws`, losing the cause, discarding stack traces, and using exceptions for flow control are catalogued with the reasoning behind why each hurts maintainability.
- **The checked-exception debate has two coherent sides** (Late): Proponents cite enforced handling and static control; critics cite cluttered code, refactoring pain, scalability issues, and awkwardness — the book presents both rather than declaring a winner.
- **Real APIs disagree with each other** (Ending): JDK, Hibernate, Spring, Hadoop, and AWS each handle exceptions differently, showing that "best practice" is contextual and evolves across library versions.
- **Modern systems expose new weaknesses** (Ending): Non-"true path" cases, lack of a systemic approach, and rising complexity mean exception handling remains an unsolved area rather than a settled one.
【Reading Tips】
- **Deep-read the Middle section on control flow and try-with-resources.** These are the parts most likely to contradict your assumptions and cause subtle production bugs; the suppressed-exception and closing-order rules are worth re-reading.
- **Skim the history chapter if you're impatient, but return to it later.** It's context, not mechanics — useful for understanding *why* Java made its choices, less so for day-to-day coding.
- **Treat the two-views chapter as a decision framework, not a verdict.** Read both the pro and contra arguments and the expert positions, then decide what fits your codebase's conventions.
- **Use the real API examples as a comparison exercise.** When reading each API's approach, ask whether your own project's exception strategy resembles it and where it diverges.
- **Keep the anti-patterns list handy as a review checklist.** It's the most immediately actionable part of the book for improving existing code.
【Coverage Limits】
This guide is synthesized from stratified excerpts covering the preface, table of contents, and portions of the history, essentials, and control-flow chapters; the later patterns, two-views, and real-API chapters are represented mainly through the contents listing and brief mentions, so specific arguments and examples from those chapters are not detailed here.
Excerpt 1
rocessing Basic Blocks try Resource Specification java.lang.AutoCloseable close() catch finally try-catch-finally try-with-resources Transfer of Control Chal...
the Java programming language offers for exception handling. This chapter intentionally does not cover what is considered right or wrong with regards to usin...
thrown inside of it. try { … } This is a regular try block. The purpose of the try block is to execute and guard a code that may potentially throw an excepti...
s added to the execution flow. What happens in these cases? Next, we cover the most common situations when an exception can be thrown, or a program executio...
d if the calling thread is not allowed to terminate the JVM. In addition, they may throw other runtime exceptions and errors. For example, java.lang.OutOfMem...
er after erasure will replace both T and U with IOException. The code will be equivalent to something like: public void intercept() { try { … } catch (IOExce...
Support this siteYour recognition and a small knowledge-service contribution help keep this technical work open source.
Scan the WeChat Pay or Alipay code below. Logged-in and guest visitors can both tip.
WeChat PayAlipay
Open WeChat or Alipay and scan. No login required.
Add Tag
Enter tag name (max 50 characters)
Share E-Book
Exceptions in Java Basics, advanced concepts, and real API examples (Nik Lumi)(Z-Library)
Scan QR code with your phone to access
Copy the link or scan the QR code to access this e-book on your phone
Share E-Book via Email
Please enter email address
Donation Statistics
¥.00
Total Donations
0
Donation Count
Exceptions in Java Basics, advanced concepts, and real API examples (Nik Lumi)(Z-Library)
Find Your Favorite Books
Only registered users can comment after logging in. Comments need to be reviewed by administrators before being displayed
Loading comments...
Reply to Comment
Edit Comment