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.
- Creating an instance of an exception class
- The default
exception constructor takes no arguments.
- Several others do.
- Make sure to look this up when you use it.
- If an exception is thrown in a constructor, the class instance is never created.
- And you need to be careful to clean up any resources you have allocated in the constructior.
- If an exception is thrown in a destructor, the program is terminated
- A function can be marked
noexcept , just like it can be marked const.
- If it is marked noexcept, and it throws an exception, the program calls
abort automatically.
- Apparently by default all destructors are marked
noexcept by default, so I don't know if my code demonstrates anything real.
- Use a catch, re-throw to free any dynamic memory in a function that does not handle exceptions.
- See throw.cpp.
- See classThrow.cpp.