Exception Handling - C P P

  • try
    • Invokes a function that may contain an exception and throws error to catch
          try {
              statement throw var;
          }
      
  • throw
    • String error is catched as character
          throw invalid_argument("message");
          catch(invalid_argument &e) {
              cout << e.what();
          }
      
  • catch
    • Catches and handles an exception of same data type
          catch(dataType var) {
              // statement
          }
      
    • Catches and handles all exception
          catch(...) {
              // statement
          }
      
  • Stack unwinding
    • If exception is thrown in a try block (or in a function that is called from a try block or in a function that is called from a function that is called from a try block and so on), all local objects allocated from the stack after the try block was entered are released (go out of scope) and their destructors are called
Share: