C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
 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.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
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.
 Both System.out and System.err are streams—a sequence of bytes.  System.out (the standard output stream) displays output  System.err (the standard.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Exception Handling: A Deeper.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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
1 CSC241: Object Oriented Programming Lecture No 28.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
© Copyright Eliyahu Brutman Exceptions. © Copyright Eliyahu Brutman Exceptions and Design Patterns - 2 Introduction to Exception Handling Definition:
Rossella Lau Lecture 9, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 9: Application with Exception Handling 
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
Java Software Solutions Foundations of Program Design Sixth Edition
1 Exception Handling Introduction to Exception Handling Exception Handling in PLs –Ada –C++ –Java Sebesta Chapter 14.
Object Oriented Programming
CIS 270—Application Development II Chapter 13—Exception Handling.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 13 - Exception Handling Outline 13.1Introduction 13.2When Exception Handling Should Be Used.
Chapter 12: Exception Handling
C++ Exceptions STL Vector. Example int Quotient (int numer, int denom} { if (denom != 0) return (numer/denom); else //What to do?? }
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  Due to poor understanding of the problem and solution.
Java Programming: Guided Learning with Early Objects
Exceptions Handling Exceptionally Sticky Problems.
VB.Net - Exceptions Copyright © Martin Schray
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
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.
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
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.
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.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Exceptions an unusual condition – e.g. division by zero – e.g. file doesn't exist – e.g. illegal type – etc. etc… typically a run-time error – i.e. during.
C++ Exceptions.
Chapter 16 Exception Handling: A Deeper Look
CS212: Object Oriented Analysis and Design
Handling Exceptionally Sticky Problems
Exceptions C++ Interlude 3
Chapter 14: Exception Handling
Exception Handling Chapter 9.
Chapter 17 Templates and Exceptions Part 2
Exception Handling Chapter 9 Edited by JJ.
Lecture 11 Objectives Learn what an exception is.
Handling Exceptionally Sticky Problems
Exception Handling.
Java Programming: From Problem Analysis to Program Design, 4e
Presentation transcript:

C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling

C++ Programming: Program Design Including Data Structures, Fourth Edition2 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 handle exceptions Become familiar with C++ exception classes

C++ Programming: Program Design Including Data Structures, Fourth Edition3 Objectives (continued) Learn how to create your own exception classes Discover how to throw and rethrow an exception Explore stack unwinding

C++ Programming: Program Design Including Data Structures, Fourth Edition4 Exceptions Exception −undesirable event detectable during program execution Until now, if exceptions occurred during execution: −Programmer-supplied code terminated the program, or −Program terminated with an appropriate error message Can add exception-handling code at the point where an error can occur

C++ Programming: Program Design Including Data Structures, Fourth Edition5 Handling Exceptions within a Program Function assert ( ) −Checks if an expression meets certain condition(s) −If conditions are not met, it terminates the program Example: division by 0 −If divisor is zero, assert terminates the program with an error message

C++ Programming: Program Design Including Data Structures, Fourth Edition12 C++ Mechanisms of Exception Handling The try / catch block handles exceptions Exception must be thrown in a try block and caught by a catch block C++ provides support to handle exceptions via a hierarchy of classes

C++ Programming: Program Design Including Data Structures, Fourth Edition13 try / catch Block Statements that may generate an exception are placed in a try block The try block also contains statements that should not be executed if an exception occurs The try block is followed by one or more catch blocks

C++ Programming: Program Design Including Data Structures, Fourth Edition14 try / catch Block (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition15 try / catch Block (continued) The catch block: −Specifies the type of exception it can catch −Contains an exception handler If the heading of a catch block contains... (ellipses) in place of parameters −Block can catch exceptions of all types If no exception is thrown in a try block −All catch blocks are ignored −Execution resumes after the last catch block

C++ Programming: Program Design Including Data Structures, Fourth Edition16 try / catch Block (continued) If an exception is thrown in a try block −Remaining statements (in block) are ignored Program searches catch blocks in order, looking for an appropriate exception handler −If the type of thrown exception matches the parameter type in one of the catch blocks: Code of that catch block executes Remaining catch blocks are ignored

C++ Programming: Program Design Including Data Structures, Fourth Edition17 try / catch Block (continued) Consider the following block: In this catch block: − x is the catch block parameter − int specifies that block can catch an exception of type int A catch block can have at most one catch block parameter

C++ Programming: Program Design Including Data Structures, Fourth Edition18 Throwing an Exception For try / catch to work, the exception must be thrown in the try block General syntax: where expression is a constant value, variable, or object The object being thrown can be a specific object or an anonymous object In C++, an exception is a value −constant, variable, object

anonymous object

C++ Programming: Program Design Including Data Structures, Fourth Edition20 Order of catch Blocks catch block can catch: −All exceptions of a specific type −All types of exceptions A catch block with an ellipses (three dots) catches any type of exception −If used, it should be the last catch block of that sequence Be careful about the order in which you list catch blocks

C++ Programming: Program Design Including Data Structures, Fourth Edition27 Using C++ Exception Classes C++ provides support to handle exceptions via a hierarchy of classes The virtual function what returns the string containing the exception object thrown by C++’s built-in exception classes The class exception is: −The base class of the exception classes provided by C++ −Contained in the header file exception

Exception Class Hierarchy C++ Programming: Program Design Including Data Structures, Fourth Edition28

C++ Programming: Program Design Including Data Structures, Fourth Edition29 Using C++ Exception Classes (continued) Two subclasses of exception (defined in stdexcept ): − logic_error invalid_argument : illegal arguments used in a function call out_of_range : string subscript out of range error length_error : if a length greater than the maximum allowed for a string object is used − runtime_error Examples: overflow_error underflow_error

C++ Programming: Program Design Including Data Structures, Fourth Edition32 Using C++ Exception Classes (continued) If new cannot allocate memory space, it throws a bad_alloc exception

C++ Programming: Program Design Including Data Structures, Fourth Edition33 Using C++ Exception Classes (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition34 Creating Your Own Exception Classes Programmers can create exception classes to handle their own exceptions −C++ uses the same mechanism to process these exceptions To throw your own exceptions, use the throw statement Any class can be an exception class

C++ Programming: Program Design Including Data Structures, Fourth Edition37 Creating Your Own Exception Classes (continued) Exception class with member variables typically includes: constructors, function what

C++ Programming: Program Design Including Data Structures, Fourth Edition 42 Rethrowing and Throwing an Exception When an exception occurs in a try block, control immediately passes to one of the catch blocks, which either: −Handles the exception or partially processes the exception and then rethrows the same exception −Rethrows a different exception for the calling environment to handle

C++ Programming: Program Design Including Data Structures, Fourth Edition43 Rethrowing and Throwing an Exception (continued) The general syntax to rethrow an exception caught by a catch block is: (in this case, the same exception is rethrown) OR where expression is a constant value, variable, or object

C++ Programming: Program Design Including Data Structures, Fourth Edition44 Rethrowing and Throwing an Exception (continued) exception clause / throw list The object being thrown can be: −specific object −anonymous object A function specifies the exceptions it throws in its heading using the throw clause

C++ Programming: Program Design Including Data Structures, Fourth Edition48 Exception Handling Techniques When an exception occurs, the programmer usually has three choices: −Terminate the program −Fix (Include code to recover from the exception) −Log the error and continue

C++ Programming: Program Design Including Data Structures, Fourth Edition49 Terminate the Program In some cases, it is best to let the program terminate when an exception occurs For example, if the input file does not exist when the program executes −There is no point in continuing with the program The program can output an appropriate error message and terminate

C++ Programming: Program Design Including Data Structures, Fourth Edition50 Fix the Error and Continue In some cases, you will want to handle the exception and let the program continue For example, if a user inputs a letter in place of a number −The input stream will enter the fail state You can include the necessary code to keep prompting the user to input a number until the entry is valid

C++ Programming: Program Design Including Data Structures, Fourth Edition51 Log the Error and Continue Example: if your program is designed to run a nuclear reactor or continuously monitor a satellite −It cannot be terminated if an exception occurs When an exception occurs −The program should write the exception into a file and continue to run

C++ Programming: Program Design Including Data Structures, Fourth Edition55 Stack Unwinding (function end) When an exception is thrown in a function, the function can do the following: −Do nothing −Partially process the exception and throw the same exception or a new exception −Throw a new exception In each of these cases, the function-call stack is unwound −The exception can be caught in the next try / catch block

C++ Programming: Program Design Including Data Structures, Fourth Edition56 Stack Unwinding (continued) When the function call stack is unwound −The function in which the exception was not caught and/or rethrown terminates −Memory for its local variables is destroyed The stack unwinding continues (in calling functions) until: −A try / catch handles the exception or −The program does not handle the exception The function terminate is called to terminate the program

C++ Programming: Program Design Including Data Structures, Fourth Edition62 Summary Exception −an undesirable event detectable during program execution assert checks whether an expression meets a specified condition; terminates if not met try / catch block handles exceptions −Statements that may generate an exception are placed in a try block − catch block specifies the type of exception it can catch and contains an exception handler

C++ Programming: Program Design Including Data Structures, Fourth Edition63 Summary (continued) If no exceptions are thrown in a try block, all catch blocks for that try block are ignored −Execution resumes after the last catch block Data type of catch block parameter specifies type of exception that catch block can catch catch block can have at most one parameter exception −base class for exception classes what returns string containing the exception object thrown by built-in exception classes

C++ Programming: Program Design Including Data Structures, Fourth Edition64 Summary (continued) Class exception is in header file exception runtime_error handles run-time errors You can create your own exception classes A function specifies the exceptions it throws in its heading using the throw clause If the program does not handle the exception, then the function terminate terminates the program