1 CSC241: Object Oriented Programming Lecture No 27.

Slides:



Advertisements
Similar presentations
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Advertisements

Lecture 9. 2 Exception  An exception is a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring.
1 Chapter 17-2 Templates and Exceptions Dale/Weems.
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.
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
Exceptions OO Software Design and Construction Computer Science Dept Va Tech January 2002 ©2002 McQuain WD & Keller BJ 1 Exceptions exceptiona program.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
CSE 332: C++ exceptions Overview of C++ Exceptions Normal program control flow is halted –At the point where an exception is thrown The program call stack.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Objectives In this chapter you will: Learn what an exception is Learn how to handle exceptions within a program See how a try / catch block is used to.
Copyright © 2012 Pearson Education, Inc. Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Lesson 16 Exceptions Lesson Exceptions1. Murphy’s Law Anything that can go wrong will go wrong Lesson Exceptions2.
Jerry Lebowitz. Topics  Provides a facility for a systematic object oriented approach to handling runtime errors ◦ Can also handle runtime errors.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
1 CSC241: Object Oriented Programming Lecture No 28.
Understand Error Handling Software Development Fundamentals LESSON 1.4.
CSI 3120, Exception handling, page 1 Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8 th ed., 2007 Dr. Nathalie.
CS Advanced C++ Exception Handling Topic #5.
© Copyright Eliyahu Brutman Exceptions. © Copyright Eliyahu Brutman Exceptions and Design Patterns - 2 Introduction to Exception Handling Definition:
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
OOP Spring 2007 – Recitation 81 Object Oriented Programming Spring 2007 Recitation 8.
Exceptions Objectives At the conclusion of this lesson, students should be able to Explain the need for exceptions Correctly write programs that use.
1 Exception and Event Handling (Based on:Concepts of Programming Languages, 8 th edition, by Robert W. Sebesta, 2007)
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Introduction to C++ Templates and Exceptions l C++ Function Templates l C++ Class Templates l Exception and Exception Handler.
Object Oriented Programming
C++ Exceptions STL Vector. Example int Quotient (int numer, int denom} { if (denom != 0) return (numer/denom); else //What to do?? }
Chapter 13. Binary Files In binary files we do not need to format data File streams include two member functions specifically designed to input and output.
Slides Credit Umair Javed LUMS Web Application Development.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  Due to poor understanding of the problem and solution.
1 CSC241: Object Oriented Programming Lecture No 13.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
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.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
HANDLING EXCEPTIONS Chapter 9. Outline  Learn about the limitations of traditional error-handling methods  Throw exceptions  Use try blocks  Catch.
CS212: Object Oriented Analysis and Design Lecture 20: Exception Handling-II.
1 CSC241: Object Oriented Programming Lecture No 02.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
LECTURE LECTURE 14 Exception Handling Textbook p
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Exception Handling How to handle the runtime errors.
CSC241 Object-Oriented Programming (OOP) Lecture No. 22.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
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.
Introduction to Exceptions in Java CS201, SW Development Methods.
Section 3.3 Exceptional Situations. 3.3 Exceptional Situations Exceptional situation Associated with an unusual, sometimes unpredictable event, detectable.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Andy Wang Object Oriented Programming in C++ COP 3330
Why exception handling in C++?
Object Oriented Programming COP3330 / CGS5409
Chapter 14: Exception Handling
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 17 Templates and Exceptions Part 2
Exception Handling.
Object-Oriented Programming (OOP) Lecture No. 43
Object-Oriented Programming (OOP) Lecture No. 44
Lecture 9.
Exceptions for safe programming.
CMSC 202 Exceptions.
Exception Handling.
Presentation transcript:

1 CSC241: Object Oriented Programming Lecture No 27

2 Previous Lecture Functional templates – Example program – multiple arguments – Macros vs. template Class templates – Stack template class – Default and non type parameter for class template – Explicit specialization

3 Today’s Lecture Exception Exception handling try, catch and throw block Multiple exceptions

4 Exceptions Exceptions are errors that occur at runtime Exception handling provide a systematic, object- oriented approach to handling errors generated by C++ classes For example, – running out of memory, – not being able to open a file, – trying to initialize an object to an impossible value, – or using an out-of-bounds index

5 Why Do We Need Exception handling C-language programs signal an error by returning a value from the function For example, disk-file functions often return NULL or 0 to signal an error if( somefunc() == ERROR_RETURN_VALUE ) //handle the error else //proceed normally if( anotherfunc() == NULL ) //handle the error else //proceed normally if( thirdfunc() == 0 ) //handle the error else //proceed normally

6 Cont. Problem : Every single call to such a function must be examined by the program – if else block and writing statement to handle error requires more code and make file size larger More complex problem: classes are used, – errors may take place without a function being explicitly called – How application know if there is error in constructor SomeClass obj1, obj2, obj3;

7 Exception syntax Imagine an application that creates and interacts with objects of a class Usually the application’s calls to the class member functions cause no problems Sometimes, the application makes a mistake, causing an error to be detected in a member function This member function informs the application that an error has occurred When exceptions are used, this is called throwing an exception.

8 Cont. A separate section of code to handle the errors is called exception handler or catch block – catch block catches the exceptions thrown by the member function In program the code uses objects of the class is enclosed in a try block. Errors generated in the try block will be caught in the catch block. Code that doesn’t interact with the class need not be in a try block The exception mechanism uses three new C++ keywords: throw, catch, and try

9 The exception mechanism

10 Program skeleton class Aclass { public: class AnError //exception class { }; void Func() { if( /* error condition */ ) throw AnError(); } }; main() { try { AClass obj1; obj1.Func(); } catch(AClass::AnError) { //tell user about error } } try block contain the code that may cause exception If error occur in Func() an exception is thrown 12 3 When exception is thrown control is tranfer to catch block

11 class Stack { private: int st[3]; int top; public: class Range { }; Stack() { top = -1; } void push(int var) { if(top >= 3-1) throw Range(); st[++top] = var; } int pop() { if(top < 0) throw Range(); return st[top--]; } }; main() { Stack s1; try { s1.push(11); s1.push(22); s1.push(33); s1.push(44); cout << "1: " << s1.pop() << endl; cout << "2: " << s1.pop() << endl; cout << "3: " << s1.pop() << endl; cout << "4: " << s1.pop() << endl; } catch(Stack::Range) { cout << "Exception: Stack Full or Empty"; } cout << "Arrive here"; } Simple example – Stack Go to program

12 Specifying the Exception Class Body of the class is empty, so objects of this class have no data and no member functions Simple example require class name Range to connect a throw statement with a catch block class Range { //note: empty class body }; class Range { //note: empty class body }; throw Range(); It will create an object of Range class Transfer the control to exception handler

13 Exception Handler (Catch Block) exception class name must include the class in which it is located It must immediately follow by try block catch(Stack::Range) { //code that handles the exception } catch(Stack::Range) { //code that handles the exception } Exception class name

14 Sequence of Events 1.Code is executing normally outside a try block. 2.Control enters the try block. 3.If a statement in the try block causes an error in a member function. 4.Then member function throws an exception. 5.Control transfers to the exception handler (catch block) following the try block

15 Multiple exceptions Stack class only throw one exception in both cases, either stack is empty or full It can be modified to throw an any number of exceptions Stack class can be modify to throw separate exceptions for attempting to push data on a full stack and attempting to pop data from an empty stack.

16 class Stack { private: int st[3]; int top; public: class Full { }; class Empty { }; Stack() { top = -1; } void push(int var) { if(top >= 3-1) throw Full(); st[++top] = var; } int pop() { if(top < 0) throw Empty(); return st[top--]; } }; main() { Stack s1; try { s1.push(11); s1.push(22); s1.push(33); s1.push(44); cout << "1: " << s1.pop() << endl; cout << "2: " << s1.pop() << endl; cout << "3: " << s1.pop() << endl; cout << "4: " << s1.pop() << endl; } catch(Stack::Full) { cout << "Exception: Stack Full"; } catch(Stack::Empty) { cout << "Exception: Stack Empty"; } } Multiple exceptions Go to program

17 Note A separate catch block is used for each exception Only one catch block is activated for a given exception A group of catch blocks, or a catch ladder is executed a little like a switch statement