1 CSC241: Object Oriented Programming Lecture No 28.

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Exception Handling Outline 13.1 Introduction 13.2 Exception-Handling Overview 13.3 Other.
Advertisements

Exception Handling Introduction Try Throw Catch. Exception Handling Use exception handling to write programs that are –Clearer –More robust –More fault-tolerant.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
1 Handling Exceptions COSC 1567 C++ Programming Lecture 11.
 2006 Pearson Education, Inc. All rights reserved. Exception Handling in C++ CS-2303, C-Term Exception Handling in C++ CS-2303 System Programming.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 23 - Exception Handling Outline 23.1Introduction 23.2When Exception Handling Should Be Used 23.3Other.
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.
Dale Roberts Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Exception Handling: A Deeper.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Jerry Lebowitz. Topics  Provides a facility for a systematic object oriented approach to handling runtime errors ◦ Can also handle runtime errors.
C++ Exception Handling
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
DYNAMIC MEMORY MANAGEMENT. int x; When the source code containing this statement is compiled and linked, an executable file is generated. When the executable.
Exception Handling Introduction Exception handling is a mechanism to handle exceptions. Exceptions are error like situations. It is difficult to decide.
CS Advanced C++ Exception Handling Topic #5.
. Plab – Tirgul 10 Exceptions. Error handling in C Up to now we handled our errors in the “C way”: assert return codes global error variable ( errno and.
Rossella Lau Lecture 9, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 9: Application with Exception Handling 
Exception Handling An Exception is an indication of a problem that occurs during a program’s execution. Exception handling enables the programmer to create.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
Plab – Tirgul 8 Exceptions. Error handling in C Up to now we handled our errors in the “C way”: assert return codes global error variable ( errno and.
 2006 Pearson Education, Inc. All rights reserved Exception Handling.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Dale Roberts Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
CPSC 252 Exception Handling Page 1 Exceptions and exception handling Client programmers can make errors using a class attempting to dequeue an item from.
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 13 - Exception Handling Outline 13.1Introduction 13.2When Exception Handling Should Be Used.
1 CSC241: Object Oriented Programming Lecture No 27.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  Due to poor understanding of the problem and solution.
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 Programmers must deal with errors and exceptional situations: User input errors Device errors Empty disk space, no memory Component.
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.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 23 - Exception Handling Outline 23.1Introduction.
Exception Handling in C++. Outline What exceptions are and when to use them Using try, catch and throw to detect, handle and indicate exceptions, respectively.
Exception Handling Outline 23.1 Introduction
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
1 CSC241: Object Oriented Programming Lecture No 05.
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
1 CSC241: Object Oriented Programming Lecture No 03.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
Exception Handling How to handle the runtime errors.
EE4E. C++ Programming Lecture 6 Advanced Topics. Contents Introduction Introduction Exception handling in C++ Exception handling in C++  An object oriented.
Lecture 18B Exception Handling and Richard Gesick.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
C ++ MULTIPLE CHOICE QUESTION
Exception handling.
Exception Handling in C++
IS 0020 Program Design and Software Tools
Exceptions Error Handling and Recovery
Chapter 16 Exception Handling: A Deeper Look
CS212: Object Oriented Analysis and Design
Exception Handling: A Deeper Look
Why exception handling in C++?
Part IX Fundamentals of C and C++ Programming Exception Handling
Chapter 14: Exception Handling
Exception Handling and
Advanced C++ Exception Handling
Exception Handling.
DYNAMIC MEMORY MANAGEMENT
Object-Oriented Programming (OOP) Lecture No. 44
16 Exception Handling.
Department of Computer and Information Science, School of Science, IUPUI Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI
Exceptions for safe programming.
Presentation transcript:

1 CSC241: Object Oriented Programming Lecture No 28

2 Previous Lecture Exception Exception handling – Why exception handling ? try, catch and throw block – Program skeleton – Example program stack ( class Range { }; ) Multiple exceptions – class Full { }; – class Empty { };

3 Today’s Lecture Exception in Distance class Re-throwing an exception Exception with arguments bad_alloc class set_new_handler function

4 class Distance { private: int feet; float inches; public: class InchesEx { }; Distance() { feet = 0; inches = 0.0; } Distance(int ft, float in) { if(in >= 12.0) throw InchesEx(); feet = ft; inches = in; } void getdist() { cout > feet; cout > inches; if(inches >= 12.0) throw InchesEx(); } }; Exceptions with the Distance Class Go to program

5 Re-throwing an Exception An exception handler (catch block) when receive an exception may decide that – It cannot process that exception or – It can process the exception only partially It might re-throw an exception to another exception handler via throw statement Go to program

6 Exceptions with Arguments What happens if the application needs more information about what caused an exception? For example – In the Distance example, it is useful to display what the bad inches value actually was – If the same exception is thrown different member functions, it be useful to display which function causes that exception to be thrown Two things happened when an exception is thrown – transferring control to the handler (catch block) – creating an object of the exception class by calling its constructor throw Full(); throw Empty();

7 class Distance { private: int feet; float inches; public: class InchesEx { public: string origin; float iValue; InchesEx(string or, float in) { origin = or; iValue = in; } }; Distance() { feet = 0; inches = 0.0; }... Exceptions with Arguments-Distance class main() { try{ Distance dist1(17, 3.5); Distance dist2;.. } catch(Distance::InchesEx ix) { cout << “Error in “ << ix.origin << “Inches value of “ <<xi.Value << “ is too large.”; } } Go to program

8 Specifying Data in an Exception Class Data in an exception class is public so it can be accessed directly by the exception handler class InchesEx { public: string origin; float iValue; InchesEx(string or, float in) { origin = or; iValue = in; } }; class InchesEx { public: string origin; float iValue; InchesEx(string or, float in) { origin = or; iValue = in; } }; Exception class data member can be private

9 Initializing an Exception Object two-argument constructor for the Stack class in the getdist() member function throw InchesEx(“getdist() function”, inches); throw InchesEx(“2-arg constructor”, in);

10 Extracting Data from the Exception Object catch(Distance::InchesEx ix) { //access ‘ix.origin’ and ‘ix.iValue’ directly } catch(Distance::InchesEx ix) { //access ‘ix.origin’ and ‘ix.iValue’ directly } Enter feet: 7 Enter inches: 13.5 Initialization error in getdist() function Inches value of 13.5 is too large Distance dist1(17, 22.25); the resulting exception will cause this error message: Initialization error in 2-arg constructor. Inches value of is too large.

11 Constructors, Destructors and Exception Handling What happens when an error is detected in a constructor? How should an object's constructor respond when new fails to allocate memory Constructor return no value so alternative mechanism is use to indicate an error Mechanisms are – Global variable – A construct must throw an exception

12 Cont. Exceptions thrown by a constructor cause destructors to be called If an object has member objects, – an exception is thrown before the outer object is fully constructed – then destructors will be executed for the member objects that have been constructed prior to the occurrence of the exception – In case of array of objects, only destructors for the constructed objects in the array will be called

13 The bad_alloc Class Standard C++ contains several built-in exception classes. The most commonly used is bad_alloc, which is thrown if an error occurs when attempting to allocate memory with new This exception was called xalloc in earlier versions of C++. Go to program

14 set_new_handler function It is an additional feature for handling new failures It takes as argument a pointer to a function with – with no arguments – returns void set_new_handles( functionName ); This function will be called if new fails This provides a method to handling all new failure with a uniform approach

15 Cont. If new allocates memory successfully, it returns a pointer to that memory If new fails to allocate memory, then – If set_new_handler did not register a new-handler function, new throws a bad_alloc exception – If a new-handler function has been registered, the new-handler function is called

16 Task performed by handler-function 1.Make more memory available by deleting other dynamically allocated memory and return to operator new to attempt to allocate memory again. 2.Throw an exception of type bad_alloc. 3.Call function abort or exit to terminate the program

17 Example

18 Standard Library Exception Hierarchy

19 Summary An exception is an indication of a problem that occurs during a program's execution Exception handling enables programmers to create programs that can resolve problems that occur at execution Exception handling enables the programmer to remove error-handling code try block define a block of code in which exceptions might occur At least one catch handler must immediately follow a try block

20 Cont. catch handler specifies an exception parameter that represents the type of exception the catch handler can process Point in the program at which an exception occurs is called the throw point When a try block terminates, local variables defined in the block go out of scope Common examples of exceptions are – out-of-range array subscripts, – arithmetic overflow, – division by zero, – invalid function parameters and – unsuccessful memory allocations

21 Cont. Destructors are called for every object constructed in a try block before an exception is thrown If an array of objects has been partially constructed when an exception occurs, only the destructors for the constructed array element objects will be called.

22 Exercise program A queue is a data-storage device. It’s like a stack, except that, instead of being last-infirst- out, it’s first-in-first-out, like the line at a bank teller’s window. Write a class template for a queue class. Also check if the queue is full or empty and throw an exception if it is Go to program