17.2. Example: Handling an Attempt to Divide by Zero

Let’s consider a simple example of exception handling (Figs. 17.117.2). We show how to deal with a common arithmetic problem—division by zero. Division by zero using integer arithmetic typically causes a program to terminate prematurely. In floating-point arithmetic, many C++ implementations allow division by zero, in which case a result of positive or negative infinity is displayed as INF or -INF, respectively.

 1   // Fig. 17.1: DivideByZeroException.h 2   // Class DivideByZeroException definition. 3   #include <stdexcept> // stdexcept header contains runtime_error       4  5   // DivideByZeroException objects should be thrown by functions 6   // upon detecting ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.