O'Reilly logo

SAMS Teach Yourself C++ in 10 Minutes SECOND EDITION by Jesse Liberty

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

Exception Handling—A Better Way

If you take a high-level look at the example you've been building, you can see that error-handling code now dominates it. While this is not unusual for professional programs, it is inconvenient to have error-handling code interlaced with normal processing code. You might also notice that the two else blocks contain essentially the same error-handling code.

Earlier forms of the example took some steps to separate the error and non-error code by using the bang operator to keep the normal processing code near the top and error-processing code near the bottom.

However, C++ offers a better way to deal with errors, called exception handling. Under exception handling, code that encounters an error throws an exception, ...

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