O'Reilly logo

Navigating C++ and Object-Oriented Design by Gail Anderson, Paul Anderson

Stay ahead with the world's most comprehensive technology and business learning platform.

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

Start Free Trial

No credit card required

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

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

Start Free Trial

No credit card required