17.2. Example: Handling an Attempt to Divide by Zero
Let’s consider a simple example of exception handling (Figs. 17.1–17.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
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 ...