https://www.youtube.com/watch?v=_Ivd3qzgT7U
use throw keyword to signal errors
// throw <value of any data type!>;
throw 1;
throw "error";
use try catch to handle exceptions.
C++ tries to find catch block recursively. if none are found, program terminates with runtime exception.
try {
// do something..
} catch (arg1) {
// catch arg work like function args
// basic types (like int, etc.) can be caught by value
// non-basic types (classes) should be caught by const ref
// to avoid unneeded copy or slicing!
// no type conversion is done for exceptions!
// e.g. int will not be matched with double catch block!
// but casts from a derived to parent class will be performed!
} catch (arg2) {
} catch (...) {
// catch has exactly 1 arg or "..." (catch all exceptions!)
}
Exceptions can be rethrown in the catch block, but it rethrowned exception is caught by parent try block…
try {
...
} catch ([some exception]) {
// throw; -> throw the same exception again
// throw [obj]; -> throw a new exception
}
exceptions thrown in normal member functions behave as usual
exceptions thrown in constructors will not trigger destructors of the class itself! however, destructors of class member variables are triggered!