60 likes | 217 Views
Exceptions. C++ Exceptions are used to propagate unexpected or severe errors. Section of code which may have error is put into try block. Any error which occurs in block is forwarded to exception handler designed by catch clause. Normal flow control is interrupted.
E N D
C++ Exceptions are used to propagate unexpected or severe errors. Section of code which may have error is put into try block. Any error which occurs in block is forwarded to exception handler designed by catch clause. Normal flow control is interrupted.
If current function does not catch function, exception propagation continues in the calling function. • Destructors of local objects are executed as objects destroyed. Propagation continues until exception caught or main is exited.
Catch clauses May have multiple catch clauses. • Matching is done in order encountered. • Derived classes will match specifications of base classes. May use (…) as catch clause, which matches any exception.
Throw statement Exception started with throw statement. • May throw an object of any type. • Good design practice to derive exceptions from exception class. • Message passed to constructor. • provides what( ) function which returns message that is passed.
Standard exceptions in <stdexcept> • logic_error • domain_error • invalid_argument • length_error • out_of_range • runtime_error • range_error • overflow_error • underflow_error