Presentation is loading. Please wait.

Presentation is loading. Please wait.

Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.

Similar presentations


Presentation on theme: "Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception."— Presentation transcript:

1 Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception Handling Lesson No. 05

2 Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 An exception is a problem that arises during the execution of a program. A C++ exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero.

3 Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Exceptions provide a way to transfer control from one part of a program to another. C++ exception handling is built upon three keywords: try, catch, and throw.

4 Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 throw: A program throws an exception when a problem shows up. This is done using a throw keyword. catch: A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception. try: A try block identifies a block of code for which particular exceptions will be activated. It's followed by one or more catch blocks.

5 Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Assuming a block will raise an exception, a method catches an exception using a combination of the try and catch keywords. A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code, and the syntax for using try/catch looks like the following:

6 Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Syntax of try/catch try { // protected code } catch( Exception Name e1 ) { // catch block } catch( Exception Name e2 ) { // catch block}} Note: You can list down multiple catch statements to catch different type of exceptions in case your try block raises more than one exception in different situations.

7 Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Throwing Exceptions: Exceptions can be thrown anywhere within a code block using throw statements. The operand of the throw statements determines a type for the exception and can be any expression and the type of the result of the expression determines the type of exception thrown.

8 Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Example of throwing an exception when dividing by zero condition occurs: double division(int a, int b) { if( b == 0 ) { throw "Division by zero condition!"; } return (a/b); }

9 Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Catching Exceptions: The catch block following the try block catches any exception. You can specify what type of exception you want to catch and this is determined by the exception declaration that appears in parentheses following the keyword catch.

10 Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Try { // protected code } catch( Exception Name e ) { // code to handle Exception Name exception } Above code will catch an exception of Exception Name type.

11 Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 If you want to specify that a catch block should handle any type of exception that is thrown in a try block, you must put an ellipsis,..., between the parentheses enclosing the exception declaration as follows: Try { // protected code } catch( ……. ) { // code to handle Exception Name exception }

12 Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Example of throwing an exception when dividing by zero condition occurs: #include double division(int a, int b) { if( b == 0 ) { throw "Division by zero condition!"; } return (a/b); } int main () { int x = 50; int y = 0; double z = 0; try { z = division(x, y); cout << z << endl; } catch (const char* msg) { cerr << msg << endl; } return 0; } Because we are raising an exception of type const char*, so while catching this exception, we have to use const char* in catch block. If we compile and run above code, this would produce the following result: Division by zero condition!

13 Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 C++ Standard Exceptions: C++ provides a list of standard exceptions defined in which we can use in our programs. These are arranged in a parent-child class hierarchy shown below:

14 Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 #include using namespace std; int main() { int StudentAge; cout << "Student Age: "; cin >> StudentAge; Try { if(StudentAge < 0) throw; cout << "\nStudent Age: " << StudentAge << "\n\n"; } catch(...) { } cout << "\n"; return 0; }

15 Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 int main () { try { throw 5; } catch (int a) { cout << "An exception occurred!" << endl; cout << "Exception number is: " << a << endl; } getch(); }

16 Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Example Declare the variables a,b,c. Read the values a,b,c,. Inside the try block check the condition. a. if(a-b!=0) then calculate the value of d and display. b. otherwise throw the exception. Catch the exception and display the appropriate message.

17 Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 // Heloo.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include using namespace std; void main() { int a,b,c; float d; //clrscr(); cout<<"Enter the value of a:"; cin>>a; cout<<"Enter the value of b:"; cin>>b; cout<<"Enter the value of c:"; cin>>c; try { if((a-b)!=0) { d=c/(a-b); cout<<"Result is:"<<d; } else { throw(a-b); } catch(int i) { cout<<"Answer is infinite because a-b is:"<<i; } getch(); }

18 Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Example Declare and define the function test(). Within the try block check whether the value is greater than zero or not. a. if the value greater than zero throw the value and catch the corresponding exception. b. Otherwise throw the character and catch the corresponding exception. Read the integer and character values for the function test().

19 Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 void test(int x) { try { if(x>0) throw x; else throw 'x'; } catch(int x) { cout<<"Catch a integer and that integer is:"<<x; } catch(char x) { cout<<"Catch a character and that character is:"<<x; } void main() { clrscr(); cout<<"Testing multiple catches\n:"; test(10); test(0); getch(); }

20 Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Example Write a program that user is asked to enter a number. If they enter a positive number, then no exception is thrown, and the square root of the number is printed. Otherwise an exception is thrown

21 Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 int main() { cout << "Enter a number: "; double dX; cin >> dX; try // Look for exceptions that occur within try block and route to attached catch block(s) { // If the user entered a negative number, this is an error condition if (dX < 0.0) throw "Can not take sqrt of negative number"; // throw exception of type char* // Otherwise, print the answer cout << "The sqrt of " << dX << " is " << sqrt(dX) << endl; } catch (char* strException) // catch exceptions of type char* { cerr << "Error: " << strException << endl; } getch(); }

22 Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 void main() { int x = -1; // Some code cout << "Before try \n"; try { cout << "Inside try \n"; if (x < 0) { throw x; cout << "After throw (Never executed) \n"; } catch (int x ) { cout << "Exception Caught \n"; } cout << "After catch (Will be executed) \n"; getch(); }


Download ppt "Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception."

Similar presentations


Ads by Google