previous | start | next

## Exception Handling

- Problems:
  - If you don't check for the error, your program may be unreliable.
  - If you return a fake value, the program continues using the value. No one knows what the error was, only that something went wrong.
  - Many real pieces of software can't be halted, such as one that controls a medical device.
- C++ has a mechanism called exception handling. When a function detects an error, it can signal (throw an exception) a condition to some other part of the program whose job it is to deal with errors.

  >     double future_value(double initial_balance, double p, int n)
  >     {
  >        if (p < 0 || n < 0)
  >        {
  >           logic_error description("illegal future_value parameter");
  >           throw description;
  >        }
  >        return initial_balance * pow(1 + p / 100, n);
  >     }

previous | start | next