previous | start | next

## Exception Handling

- How do you handle programmer errors when using a function or class?
  - Don't check the error condition and executing the code anyway, possibly causing a problem.

    >     double future_value(double initial_balance, double p, int n)
    >     {
    >        return initial_balance * pow(1 + p / 100, n);
    >     }

  - Check the error condition and doing nothing if it occurs.

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

  - Check the error condition and alert the caller if it occurs.

    >     double future_value(double initial_balance, double p, int n)
    >     {
    >        assert(p >= 0 && n >= 0);
    >        return initial_balance * pow(1 + p / 100, n);
    >     }

previous | start | next