Creating Exceptions
Notes
- A function, which needs to generate an exception uses the
throw command.
- throw
- If called with a single argument, a new exception of that type is created
- If called with no argument, a previously caught exception is re-thrown .
- If called with no argument and no previous thrown exception, the program is terminated.
- When an exception is thrown
- all executable code in the stack is skipped until a catch occurs
- Each time a function is exited, any object instances that go out of lifetime have their destructors called.
- If an exception is thrown in a constructor, the class instance is never created.
- If an exception is thrown in a destructor, the program is terminated
- In any case, always free any dynamic memory allocated in a function that will call an exception before the exception is called.
- And use a catch, re-throw to free any dynamic memory in a function that does not handle exceptions.
- See throw.cpp.