Exception Mechanism

Exception Subclass

Inside constructor and destructor

Destructor

  • Destructor is call, when exception is thrown, for all local variables in the current scope

Destructor throw exceptions

  • If the exception is handled inside a catch in the destructor, this is safe
  • If not, there are 2 Unwinding at the same time, this is undefined behavior

Tip

Destructor should never throw exceptions
If it must, exceptions must be handle in side destructor
Usually, this should not happen, as destructor should only release resource

Constructor

  • If an exception is thrown during constructor call, the partial initialized object will be destroy
    • All data members are destroyed
    • If the object is a child class, it’s parent will be destroy as well
  • The object does not exist until the constructor call completes

Tip

Construct should not handle exception, leave it for the caller
Only if the exception can be handled and the creation can continue, does the constructor handle the exception
Constructor can throw exception to the caller

Note

C++ streams do not throw exception by default
These exception can be optional enable

// enable exception for a file
ifile.exceptions(std::ifstream::failbit | std::ifstream::badbit)
// throw exception if text.txt can not be open
ifile.open("text.txt")

Custom exceptions class

Tip

DON’T derive directly from the std::exception
Derive from the subclass of std::exception

Warning

The exception object must lightweight
Be cautious to not cause anymore exception

How

  • Constructor taking a C-string and string
  • Copy constructor
    • The custom exception object will be copied in to local stack memory
  • override What()
  • can have data members

Exception safety

Info

Code haves correctly when exception occurs
All programs should have exception safety

3 level of exception safety

  • Basic exception guarantee
    • If an exception is throw, no resource leaks
    • Files open during the operation will be closed
    • Memory allocated during the operation will be released

Info

All operations and functions in C++ standard library provide this level of safety

  • Strong exception guarantee
    • If exception happens, the program reverts to its previous state, as if the exception did not happen

Info

All operations in STL container provide this level of safety
Except for a couple of special case of insert operations

  • No-throw guarantee
    • Code does not throw any exceptions

Info

Except new, dynamic_cast and throw, none functions or operations of C++ and standard library throw exceptions

throw()

C++98
// Can throw NetworkConnectionError and DataError
void func() throw (NetworkConnectionError, DataError){
// code
}

If an exception is not in the list, the program is terminated by default

Danger

The list of exceptions is not checked by the compiler
If the list is incorrect, the program may be terminated unexpectedly

Removal of throw()

Info

throw() was deprecated in C++11
throw() with argument list was removed from C++17
throw() with an empty argument list was remove from C++20

noexcept

  • Promise to caller that the function does not throw any exceptions
    • So that compiler can do some useful optimization
  • Declare that nothing can be done if exceptions happens, so terminate the program by default
  • No overloading on noexcept functions
  • Destructor is implicitly noexcept

Info

Exception-safe class