Skip to main content

13. Exceptions and RAII

16/03/23

Exceptions

  • Are thrown to report exceptional circumstances
  • Can throw any type of object, fundamental type or pointer as exception in C++
  • You add handler code to catch the exception
  • The stack frame is unwound, one function at a time until a catch which matches the type thrown is found

catch caluse

  • A catch clause will match an exception of the specified type
  • catch clauses are checked in the order in which they are encountered
  • Pointers and objects are different
  • Exceptions are thrown by value

RAII (Resource Acquisition Is Initialisation)

  • On initialisation, get the resource
  • On destruction, release the resource