previous | start | next

## Exception Handling

- It's a good idea to use inheritance to define your own exception types.

  >     class FutureValueError : public logic_error
  >     {

  >     public:
  >        FutureValueError(const char reason[]);
  >     };

  >     FutureValueError::FutureValueError(const char reason[])
  >        : logic_error(reason){}

- Here, the `future_value` function can now throw a `FutureValueError` object.

  >     if (p < 0 || n < 0)
  >        throw FutureValueError("illegal parameter");

- Because `FutureValueError` is a `logic_error`, you can still catch it with `catch (logic_error& e)`.
- You can even catch other types of errors. Note that the exception handling mechanism matches the handlers top to button, and executes the first matching handler.

  >     try
  >     {
  >        code
  >     }
  >     catch (FutureValueError& e)
  >     {
  >        handler1
  >     }
  >     catch (logic_error& e)
  >     {
  >        handler2
  >     }

previous | start | next