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

Slides:



Advertisements
Similar presentations
Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
Advertisements

Topics Introduction Types of Errors Exceptions Exception Handling
Lecture 9. 2 Exception  An exception is a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring.
Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
Mahmoud Rafeek Alfarra Computer Programming || Chapter 2: Exception handling.
Understand Error Handling Software Development Fundamentals LESSON 1.4.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution.
CS Advanced C++ Exception Handling Topic #5.
True or false A variable of type char can hold the value 301. ( F )
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Exceptions COMPSCI 105 S Principles of Computer Science.
Object Oriented Programming
C++ Exceptions STL Vector. Example int Quotient (int numer, int denom} { if (denom != 0) return (numer/denom); else //What to do?? }
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  Due to poor understanding of the problem and solution.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Chapter 4 Selection Structures: Making Decisions.
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
Lecture #7 CONTROL STRUCTURE & FLOW CHARTS
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
1 Original Source : and Problem and Problem Solving.ppt.
HANDLING EXCEPTIONS Chapter 9. Outline  Learn about the limitations of traditional error-handling methods  Throw exceptions  Use try blocks  Catch.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 06 FUNCTIONS 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
Recap……Last Time [Variables, Data Types and Constants]
LECTURE LECTURE 14 Exception Handling Textbook p
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
Exception Handling How to handle the runtime errors.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
EE4E. C++ Programming Lecture 6 Advanced Topics. Contents Introduction Introduction Exception handling in C++ Exception handling in C++  An object oriented.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 07: Object Oriented Programming:2014 Object-Oriented Programming in C++ Operator.
C++ Lesson 1.
Basic concepts of C++ Presented by Prof. Satyajit De
Exception handling.
Abstract Class Abstract Class is a class which contains atleast one Pure Virtual function in it. Abstract classes are used to provide an Interface for.
Exceptions Exceptions are used to signal that an unexpected event has happened in a program C++ will generate exceptions for some errors in the program.
Andy Wang Object Oriented Programming in C++ COP 3330
Why exception handling in C++?
Part IX Fundamentals of C and C++ Programming Exception Handling
EXCEPTION HANDLING.
Object Oriented Programming COP3330 / CGS5409
Andy Wang Object Oriented Programming in C++ COP 3330
Summary Two basic concepts: variables and assignments Basic types:
Exception Handling.
By Hector M Lugo-Cordero September 3, 2008
Object-Oriented Programming (OOP) Lecture No. 43
Functions Imran Rashid CTO at ManiWeber Technologies.
Object-Oriented Programming (OOP) Lecture No. 44
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Chapter 1 c++ structure C++ Input / Output
Exceptions Review Checked Vs. Unchecked Exceptions
Presentation transcript:

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

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.

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.

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.

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:

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.

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.

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); }

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.

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.

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 }

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!

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:

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; }

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(); }

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.

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(); }

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().

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(); }

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

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(); }

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(); }