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.

Slides:



Advertisements
Similar presentations
Topics Introduction Types of Errors Exceptions Exception Handling
Advertisements

 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Exception Handling Outline 13.1 Introduction 13.2 Exception-Handling Overview 13.3 Other.
Exception Handling. Introduction Errors can be dealt with at place error occurs –Easy to see if proper error checking implemented –Harder to read application.
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.
Exception Handling Handling an unexpected behaviour Unit - 08.
 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.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
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
1 CSC241: Object Oriented Programming Lecture No 28.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution.
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.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
 2006 Pearson Education, Inc. All rights reserved Exception Handling.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
Dale Roberts Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
CPSC 252 Exception Handling Page 1 Exceptions and exception handling Client programmers can make errors using a class attempting to dequeue an item from.
 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.
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.
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.
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.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Exceptions and Program Correctness based on the original work by Dr. Roger deBry Version 1.1.
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.
LECTURE LECTURE 14 Exception Handling Textbook p
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.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Exception handling.
Exception Handling in C++
IS 0020 Program Design and Software Tools
Exceptions Error Handling and Recovery
16 Exception Handling.
Chapter 16 Exception Handling: A Deeper Look
CS212: Object Oriented Analysis and Design
Jim Fawcett CSE687 – Object Oriented Design Spring 2001
Exception Handling: A Deeper Look
Why exception handling in C++?
Part IX Fundamentals of C and C++ Programming Exception Handling
Chapter 14: Exception Handling
Advanced C++ Exception Handling
16 Exception Handling.
Department of Computer and Information Science, School of Science, IUPUI Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI
ENERGY 211 / CME 211 Lecture 24 November 14, 2008.
Presentation transcript:

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 the consequences. –Exception handling is used in situations in which the system can recover from the error causing the exception. The recovery procedure is the exception handler.

Exception Handling –Exception handling is designed to handle synchronous errors. Divide by zero, array out of bounds –Exception handling is not intended to handle asynchronous errors. Disk I/O completions, networking messages, mouse clicks, etc. –Exception handling should only be used to process exceptional situations.

Exception Handling C++ exception handling is similar in concept to Java exception handling.

Exception Handling A function will throw an exception when it detects an error and is unable to deal with the error. An exception handler processes exceptions. –There may not be an exception handler for every exception. –If there is an exception handler, the exception is caught and handled.

Exception Handling A try block is used to enclose the code that may generate an error that will produce an exception. At least one catch block follows a try block. –A catch block specifies the type of exception it can catch and handle. –Each catch block contains an exception handler.

Exception Handlers If there is no catch block for the exception, the terminate function is called. –The terminate function calls the abort function that aborts the program.

Try/Catch Blocks If no exceptions are thrown for a particular try block, then the catch block(s) are skipped. –The program continues with the first statement after the catch block(s).

Try/Catch Blocks If an exception is thrown for a particular try block, then the rest of the try block statements are skipped and the appropriate catch block is executed. –The program continues with the first statement after the catch blocks.

Try Blocks Each try block begins with the keyword try. –The block is then enclosed in braces. try{ int result = quotient(number1, number2); cout << “The quotient is: “ << result << endl; }

Catch Blocks Each catch block begins with the keyword catch followed by parenthesis containing an exception type and possibly a parameter name. –The block is enclosed with braces.

Catch Blocks catch(DivideByZeroException ex) { cout << “Exception occurred:” << ex.what() << ‘\n\’; } Note, the above assumes the exception is defined.

Catch Blocks A particular catch block is executed if the block’s parameter type matches the thrown object’s type. A catch statement followed by parenthesis enclosing an ellipsis will catch all exceptions. catch (…) { }

Catch Blocks The type of the catch block handler parameter matches the type of the thrown object if: –They are the same type. –The catch handler parameter type is a public base class of the class of the thrown object.

Catch Blocks –The handler parameter is of a base-class pointer or reference type and the thrown object is of a derived-class pointer or reference type. –The catch handler is of the form catch(…) An exact match is required.

Throw A function can specify what exceptions to throw. –When an exception occurs, control is returned from the function and the appropriate catch block is executed. –The point at which a function throws an exception is called the throw point. –Once a function throws an exception, control can not be returned to that function.

Throw If a function throws a const object then the catch handler argument type must be declared const as well. Catch blocks can discover an error and throw an exception. If a function throws an exception, the function terminates, all local variables are destroyed, and control returns to the point at which that function was called.

Throw Exceptions in C++ can also throw exceptions that are not objects. –C++ can throw exceptions using primitive data types.

Try/Catch/Throw Example Memory exception example that throws an int.

Rethrowing Exceptions If a catch block determines it is unable or does not want to handle and exception, the catch block can rethrow the exception. –The statement to rethrow an exception is: throw;

Rethrowing Exceptions Example of rethrowing exceptions.

Exception Specifications Any function can throw an exception. –The programmer can provide a list of specific exceptions that a function may throw. This list is referred to as the throw list. returntype functionName(params) throw (typeA, typeB, typeC) { … }

Exception Specifications If the throw() is placed after the function’s parameters list, the function will not throw any exceptions. A function with no exception specification can throw any exception.

Exception Specifications If an exception not listed in the exception specification is thrown, the function unexpected is called. –The unexpected function calls the function specified with the set_unexpected function. –If no function has been set, the unexpected function by default calls the terminate function which calls abort.

Exception Specifications If a function throws an exception of a particular class type, that function can also throw exceptions of all classes derived from that class with public inheritance.

Constructors and Destructors A thrown exception from a constructor passes to the outside world the information about the failed constructor as well as the responsibility to deal with the failure. –In order to catch the exception, the exception handler must have access to a copy constructor for the thrown object.

Constructors and Destructors Exceptions thrown in constructors cause destructors to be called for any objects built as part of the object being constructed before the exception is thrown. –If an array of objects has been partially constructed when an exception occurs, only the destructors for the constructed array elements will be called.

new Failures By default, C++ throws a bad_alloc exception when a new command fails. –Not all C++ compilers can comply with this standard and therefore, simply return 0 (zero) on a new failure. In this case, your program must either test the new object via a boolean test or an assertion. Some of these compilers can throw the exception if the header file is included.

new Failures The set_new_handler function allows one to specify a uniform method of processing every new failure. –This function takes a function pointer as it’s parameter. The function pointer must point to a function that takes no arguments and returns void.

new failures The C++ standard specifies that the new handler function should perform one of the following tasks: –Make more memory available by deleting other dynamically allocated memory and return to the loop in operator new in an attempt to allocate memory again. –Throw an exception of type bad_alloc. –Call function abort or exit to terminate the program.

Standard Library Exceptions The base class for all exceptions is exception. –This class contains the function what() that returns an error message. –Each derived class overrides what() to provide an appropriate error message.

Standard Library Exceptions The runtime_error class is derived from exception and is the base class for errors that can only be detected at execution time. –A good program will associate each type of execution time error with an appropriately named exception object. This may require defining your own exception classes. i.e. overflow_error

Standard Library Exceptions The logic_error class is also derived from exception and is the base class for errors in program logic. –i.e. invalid_argument or out_of_range The exception class is also the base class for exceptions thrown by the C++ language features. –i.e. bad_alloc, or bad_cast

Defining Exception Classes As a programmer you are able to define your own exception classes. –Such classes should contain the constructor, a what function, and a message attribute.

Defining Exception Classes Class DivideByZeroException{ public: DivideByZeroException() : message(“attempted to divide by zero”) {} const char *what() const { return message;} private: const char *message; };

Defining Exception Classes Once the exception class has been defined, the program simply throws the exception and catches the exception. –throws throw DivideByZeroException(); –catch catch(DivideByZeroException ex) { … }

Exceptions/Memory/Compilers Exception examples that demonstrate memory management on different compilers.