With Safari, you learn the way you learn best. Get unlimited access to videos, live online training,
learning paths, books, tutorials, and more.

13.1. Why Use Exceptions?

To see why exceptions are appealing, consider the following alternatives for error handling.

Terminate the program.

Return a value representing an error.

Return a legal value, but set a global error flag.

Call a user-supplied error function.

Each method has flaws. Terminating a program with exit(1) is unacceptable for embedded systems that must always run in a defined state. Returning a value that represents an error is not possible with many container classes (consider popping an empty integer Stack—what integer error code would be valid?) Returning a legal value after you set a global error code makes the tacit assumption that someone will check it. Not only are global variables error prone and unacceptable for concurrency, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training,
learning paths, books, interactive tutorials, and more.