13.1. Why Use Exceptions?

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

  1. Terminate the program.

  2. Return a value representing an error.

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

  4. 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, ...

Get Navigating C++ and Object-Oriented Design 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.