DOC PREVIEW
IUPUI CS 265 - Exception Handling

This preview shows page 1-2-3-19-20-38-39-40 out of 40 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Chapter 23 - Exception Handling23.1 Introduction23.1 Introduction (II)23.1 Introduction (III)23.2 When Exception Handling Should Be Used23.3 Other Error-Handling Techniques23.3 Other Error-Handling Techniques (II)23.4 Basics of C++ Exception Handling: try, throw, catch23.4 Basics of C++ Exception Handling: try, throw, catch (II)23.5 A Simple Exception-Handling Example: Divide by ZeroPowerPoint PresentationSlide 12Slide 1323.6 Throwing an Exception23.6 Throwing an Exception (II)23.7 Catching an Exception23.7 Catching an Exception (II)23.7 Catching an Exception (III)23.7 Catching an Exception (IV)23.8 Rethrowing an ExceptionSlide 21Slide 2223.9 Exception Specifications23.10 Processing Unexpected Exceptions23.11 Stack Unwinding23.12 Constructors, Destructors and Exception Handling23.12 Constructors, Destructors and Exception Handling (II)Slide 2823.13 Exceptions and Inheritance23.14 Processing new Failures23.14 Processing new Failures (II)Slide 32Slide 33Slide 34Slide 3523.15 Class auto_ptr and Dynamic Memory AllocationSlide 37Slide 3823.16 Standard Library Exception Hierarchy23.16 Standard Library Exception Hierarchy (II) 2000 Prentice Hall, Inc. All rights reserved.Chapter 23 - Exception HandlingOutline23.1 Introduction23.2 When Exception Handling Should Be Used23.3 Other Error-Handling Techniques23.4 Basics of C++ Exception Handling: try, throw, catch23.5 A Simple Exception-Handling Example: Divide by Zero23.6 Throwing an Exception23.7 Catching an Exception23.8 Rethrowing an Exception23.9 Exception Specifications23.10 Processing Unexpected Exceptions23.11 Stack Unwinding23.12 Constructors, Destructors and Exception Handling23.13 Exceptions and Inheritance23.14 Processing new Failures23.15 Class auto_ptr and Dynamic Memory Allocation23.16 Standard Library Exception Hierarchy 2000 Prentice Hall, Inc. All rights reserved.23.1 Introduction•Errors can be dealt with at place error occurs–Easy to see if proper error checking implemented–Harder to read application itself and see how code works•Exception handling –Makes clear, robust, fault-tolerant programs–C++ removes error handling code from "main line" of program•Common failures–new not allocating memory–Out of bounds array subscript–Division by zero–Invalid function parameters 2000 Prentice Hall, Inc. All rights reserved.23.1 Introduction (II)•Exception handling - catch errors before they occur–Deals with synchronous errors (i.E., Divide by zero)–Does not deal with asynchronous errors - disk I/O completions, mouse clicks - use interrupt processing–Used when system can recover from error•Exception handler - recovery procedure–Typically used when error dealt with in different place than where it occurred–Useful when program cannot recover but must shut down cleanly•Exception handling should not be used for program control–Not optimized, can harm program performance 2000 Prentice Hall, Inc. All rights reserved.23.1 Introduction (III)•Exception handling improves fault-tolerance–Easier to write error-processing code–Specify what type of exceptions are to be caught•Most programs support only single threads–Techniques in this chapter apply for multithreaded OS as well (windows NT, OS/2, some UNIX)•Exception handling another way to return control from a function or block of code 2000 Prentice Hall, Inc. All rights reserved.23.2 When Exception Handling Should Be Used•Error handling should be used for–Processing exceptional situations–Processing exceptions for components that cannot handle them directly–Processing exceptions for widely used components (libraries, classes, functions) that should not process their own exceptions–Large projects that require uniform error processing 2000 Prentice Hall, Inc. All rights reserved.23.3 Other Error-Handling Techniques•Use assert –If assertion false, the program terminates•Ignore exceptions–Use this "technique" on casual, personal programs - not commercial!•Abort the program –Appropriate for nonfatal errors give appearance that program functioned correctly –Inappropriate for mission-critical programs, can cause resource leaks•Set some error indicator –Program may not check indicator at all points there error could occur 2000 Prentice Hall, Inc. All rights reserved.23.3 Other Error-Handling Techniques (II)•Test for the error condition– Issue an error message and call exit–Pass error code to environment• setjump and longjump –In <csetjmp>–Jump out of deeply nested function calls back to an error handler. –Dangerous - unwinds the stack without calling destructors for automatic objects (more later)•Specific errors –Some have dedicated capabilities for handling them–If new fails to allocate memory new_handler function executes to deal with problem 2000 Prentice Hall, Inc. All rights reserved.23.4 Basics of C++ Exception Handling: try, throw, catch•A function can throw an exception object if it detects an error–Object typically a character string (error message) or class object–If exception handler exists, exception caught and handled–Otherwise, program terminates 2000 Prentice Hall, Inc. All rights reserved.23.4 Basics of C++ Exception Handling: try, throw, catch (II)•Format–Enclose code that may have an error in try block–Follow with one or more catch blocks•Each catch block has an exception handler–If exception occurs and matches parameter in catch block, code in catch block executed–If no exception thrown, exception handlers skipped and control resumes after catch blocks–throw point - place where exception occurred•Control cannot return to throw point 2000 Prentice Hall, Inc. All rights reserved.23.5 A Simple Exception-Handling Example: Divide by Zero•Look at the format of try and catch blocks•Afterwards, we will cover specifics 2000 Prentice Hall, Inc. All rights reserved.Outline1. Class definition1.1 Function definition1 // Fig. 23.1: fig23_01.cpp2 // A simple exception handling example.3 // Checking for a divide-by-zero exception.4 #include <iostream>56 using std::cout;7 using std::cin;8 using std::endl;910 // Class DivideByZeroException to be used in exception 11 // handling for throwing an exception on a division by zero.12 class DivideByZeroException {13 public:14 DivideByZeroException() 15 : message( "attempted to divide by zero" ) { }16 const char *what() const { return message; }17 private:18 const char *message;19


View Full Document

IUPUI CS 265 - Exception Handling

Download Exception Handling
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Exception Handling and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Exception Handling 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?