Exception Throwing

  • The thrown object is copied to special memory which
  • Is set up by compiler and be accessible by any catch block
    which can handle the exception type
  • Every local variable in the scope is destroyed
    • including the thrown object
  • The program immediately leave the scope
    • it doesn’t execute any further instruction in the scope

Stack Unwinding

  • The program will look for a catch block that can handle the exception
  • If it cannot find one, it will immediately destroy all local variables and exit current scope
  • It then look in the enclosing scope
  • This continues until it find a suitable handler
    • If it reaches main without a suitable handler, the program call std::terminate()

Rethrowing an Exception

  • When a suitable handler is found, the program execute the code in it and continues
  • Normally, it will proceed to the next instruction after the try/catch block and
    continue from there
  • However, the handler can “rethrow” an exception
  • In this case, the handler will be handle again but in different handler of another try/catch block
  • To rethrow, use keyword throw in a catch block