Download presentation
Presentation is loading. Please wait.
Published bySergio Ashbridge Modified over 9 years ago
1
Exception Handling Handling an unexpected behaviour Unit - 08
2
Unit Introduction This unit covers C++ exception handling mechanism
3
Unit Objectives After covering this unit you will understand… Exception handling Exception handling Throwing exceptions Throwing exceptions Catching an exception Catching an exception Termination vs. Resumption Termination vs. Resumption Exception specification Exception specification Uncaught exception Uncaught exception Exception objects Exception objects
4
Exception Handling To recover from undesirable situation To recover from undesirable situation To increase robustness of code To increase robustness of code Deal with synchronous errors Deal with synchronous errors Recovery procedure as exception handler Recovery procedure as exception handler
5
Throwing an Exception Throwing an exception as raising an exception Throwing an exception as raising an exception We can specify the exceptions a function throws We can specify the exceptions a function throws Point at which throw is executed, is called throw point Point at which throw is executed, is called throw point Use of throw keyword Use of throw keyword throw MyException(“This is My Exception”);
6
Re-throwing an Exception In catch block, an exception caught can be re- thrown to a higher level using throw In catch block, an exception caught can be re- thrown to a higher level using throw throw statement should appear in a catch handler otherwise it will cause a call to terminate throw statement should appear in a catch handler otherwise it will cause a call to terminate catch(...) // catching any general exception { cout << ”An Exception occurred”; cout << ”An Exception occurred”; throw; // re-throwing an exception throw; // re-throwing an exception}
7
Catching an Exception Use of try and catch keyword Use of try and catch keyword To solve the problem within same scope To solve the problem within same scope catch blocks, as exception handlers catch blocks, as exception handlers Only one exception handler for every exception type Only one exception handler for every exception type try {// code that may generate exceptions } catch(ExceptionType1 id1) // specific type { // to handle exceptions of type1 } catch(...) // general exception { // to handle any general exception }
8
Catching an Exception (contd.) Enclose the code in try block that can throw an exception Enclose the code in try block that can throw an exception For a try block place one or more catch blocks For a try block place one or more catch blocks In each catch block catch the required exception that need to be caught In each catch block catch the required exception that need to be caught Catching (…) means any exception will be caught Catching (…) means any exception will be caught
9
Termination vs. Resumption Two models in exception handling theory Two models in exception handling theory Unrecoverable error cause termination Unrecoverable error cause termination To resume program execution after recovering form error is resumption To resume program execution after recovering form error is resumption
10
Example: Exception Handling class BaseException { public: public: BaseException() { m_pError = “Base Exception”; } void PrintError() const { cout << m_pError; } void PrintError() const { cout << m_pError; } char* m_pError; }; class DerivedException : public BaseException { public: public: DerivedException() {m_pError = “Derived Exception”; } }; float quotient(int num1, int num2) { if(num1 == 0 && num2 == 0) if(num1 == 0 && num2 == 0) throw BaseException(); throw BaseException(); else if(num2 == 0) else if(num2 == 0) throw DerivedException();
11
Example: Exception Handling (contd.) return (float) num1 / num2; // normal flow of execution } void main() { try try { { float result = quotient(5, 0); float result = quotient(5, 0); cout << “Quotient is:” << result; cout << “Quotient is:” << result; throw 1; //throwing primitive type exception throw 1; //throwing primitive type exception} catch(DerivedException exception) { exception.printError(); exception.printError();}
12
Example: Exception Handling (contd.) catch(BaseException exception) { exception.printError(); exception.printError(); } catch(...) { cout << ”General exception”; cout << ”General exception”; throw; // re-throwing an exception throw; // re-throwing an exception } } catch(...) catch(...) { cout << “General exception again occurred”; }}
13
Exception Specification Enables a list of exceptions that can be thrown by a function to be specified Enables a list of exceptions that can be thrown by a function to be specified Exception specification is also called throw list Exception specification is also called throw list throw clause is used in function signature throw clause is used in function signature void Function() throw(DivByZero, IllegalNumber); void Function() throw(DivByZero, IllegalNumber); void Function(); void Function(); void Function() throw; void Function() throw;
14
Exception Specification (contd.) unexpected() is called if an exception is thrown that is not in exception specification which by default calls terminate() unexpected() is called if an exception is thrown that is not in exception specification which by default calls terminate() A user function can be specified when unexpected() is called by set_unexpected function A user function can be specified when unexpected() is called by set_unexpected function abort() will automatically be called after the last action of user-defined termination function abort() will automatically be called after the last action of user-defined termination function
15
Example: Exception Specification class Exception1 {}; class Exception2 {}; void UnexpectedException(); void Function(int i) throw (Exception1, Exception2) { switch(i) switch(i) { case 1:throw Exception1(); case 2:throw Exception2(); } UnexpectedException(); // calls unexpected function UnexpectedException(); // calls unexpected function} void UnexpectedException() { throw 1; } void MyTerminationFunction() {cout << “unexpected exception occurred”; }
16
Example: Exception Specification (contd.) void main() { set_unexpected(MyTerminationFunction); set_unexpected(MyTerminationFunction); // ignores return value // ignores return value for(int i = 1; i <=3; i++) for(int i = 1; i <=3; i++) try try {function(i); } catch(Exception1) catch(Exception1) { cout << "Exception1 occurred"; } catch(Exception2) catch(Exception2) { cout << "Exception2 occurred"; }}
17
Uncaught Exception If an exception occurs that has no matching catch block, terminate() is called If an exception occurs that has no matching catch block, terminate() is called terminate() is a pointer to function, by default this function is abort() terminate() is a pointer to function, by default this function is abort() set_terminate() can be used to set a user defined function, when terminate() will be called set_terminate() can be used to set a user defined function, when terminate() will be called
18
Example: Uncaught Exception void MyTerminate() { cout << "My terminate"; cout << "My terminate"; abort(); // must be aborted abort(); // must be aborted} void (*ptr_fn)()= set_terminate(MyTerminate); //ptr to fn class Outer { public: public: class Inner {}; void Function() { cout << "Outer::function()"; cout << "Outer::function()"; throw Inner(); throw Inner();}};
19
Example: Uncaught Exception (contd.) void main() { try try { Outer obj; obj.Function(); // results in call to terminate // function // function } catch(Outer) catch(Outer) { cout << "trying to catch outer exception"; } // no handler for Inner type exception // no handler for Inner type exception}
20
Exception Objects Exception to be caught can be as simple as an int and as complex as a complete class type Exception to be caught can be as simple as an int and as complex as a complete class type Exception object should be caught by reference, and not by value Exception object should be caught by reference, and not by value Exception can also be caught as a pointer Exception can also be caught as a pointer
21
Example: Catch By Reference class Parent { public: public: virtual void Print() { cout << "Parent"; cout << "Parent";}}; class Child : public Parent { public: public: void Print() { cout << "Child"; } }; void Function() { throw Child(); } // function throwing exception of type Child
22
Example: Catch By Reference (contd.) int main() { try try {Function(); } catch(Parent p) // catch by value, child object sliced catch(Parent p) // catch by value, child object sliced {p.Print(); } try try {Function(); } catch(Parent& p) //catch by reference catch(Parent& p) //catch by reference {p.Print(); }}
23
Unit Summary In this unit you have covered … In this unit you have covered … Handling of exceptions Handling of exceptions Throwing exceptions Throwing exceptions Catching an exception Catching an exception Exception specification Exception specification Uncaught exception Uncaught exception Exception objects Exception objects
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.