9.1 Basic Exception Handling

Jack, be nimble,Jack, be quick,Jack, jump over the candle stick.

—ANONYMOUS

Java provides a way to handle certain kinds of special conditions in your program. This facility enables you to divide a program or method definition into separate sections for the normal case and for the exceptional case. In this way, you treat one larger programming task as two smaller and more easily doable programming tasks.

An exception is an object that signals the occurrence of an unusual event during the execution of a program. The process of creating this object—that is, generating an exception—is called throwing an exception. You place the code that deals with the exceptional case at another place in your program—perhaps in a ...

Get Java: An Introduction to Problem Solving and Programming, 8th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.