Exceptions
Java’s roots are in embedded systems—software that runs inside specialized devices like hand-held computers, cellular phones, and fancy toasters. In those kinds of applications, it’s especially important that software errors be handled robustly. Most users would agree that it’s unacceptable for their phone to simply crash or for their toast (and perhaps their house) to burn because their software failed. Given that we can’t eliminate the possibility of software errors, it’s a step in the right direction to recognize and deal with anticipated application-level errors in a methodical way.
Dealing with errors in a language like C is entirely the responsibility of the
programmer. There is no help from the language itself in identifying
error types, and there are no tools for dealing with them easily. In
C, a routine generally indicates a failure by returning an
“unreasonable” value (e.g., the idiomatic
-1 or null). As the programmer,
you must know what constitutes a bad result, and what it means.
It’s often awkward to work around the limitations of passing
error values in the normal path of data flow.[14]
An even worse problem is that certain types of errors can
legitimately occur almost anywhere, and it’s prohibitive and
unreasonable to explicitly test for them at every point in the
software.
Java offers an elegant solution to these problems with exception handling. (Java exception handling is similar to, but not quite the same as, exception handling in C++.) An exception ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access