Ninth step for Learning C++ Programming CLASS Virtual Functions Polymorphism Exception Handling Try-catch, throwing Nested try-catch Template
[ practice 1 virtual Functions ]
[ explain 1 virtual Functions ]
[ practice 1-2 virtual Functions ]
[ explain 1-2 virtual Functions ]
[ practice 2 Polymorphism (1/2) ]
[ practice 2 Polymorphism (2/2) ]
[ explain 2 Polymorphism ]
Exception Handling In a language without exception handling Programmers are responsible for implementing some form of error detection and recovery mechanism In a language with exception handling Programs are allowed to trap specific errors by using exceptions, thereby providing the possibility of fixing the problem and continuing Exception handling separates error-handling code from other programming tasks, thus making programs easier to read and to modify. 11/2014
The exception handling code unit is called an exception handler An exception is any unusual event, either erroneous or not, detectable by either hardware or software, that may require special processing The special processing that may be required after detection of an exception is called exception handling The exception handling code unit is called an exception handler
Exceptions are raised explicitly by the statement: Throwing Exceptions Exceptions are raised explicitly by the statement: throw expression; The type of the expression disambiguates the intended handler 11/2014
[ practice 3 try-catch, throwing ]
[ explain 3 try-catch, throwing ]
[ practice 4 try-catch, throwing ]
[ explain 4 try-catch, throwing ]
[ practice 5 try-catch, throwing … continue ]
[ practice 5 try-catch, throwing ]
[ explain 5 try-catch, throwing ]
[ practice 6 Nested try-catch ]
[ explain 6 Nested try-catch ]
Exception Class – catch by reference class Exception { public: int code; Exception(int i) { code = i; } }; void foo() { try { throw Exception(1); catch (Exception &e) { std::cout << e.code; No copy of the exception object 11/2014
[ practice 7 Exception Class – catch by reference ]
[ explain 7 Exception Class – catch by reference ]
Macro & Template
[ practice 8 Template ]
[ explain 8 Template ]