Jerry Lebowitz. Topics  Provides a facility for a systematic object oriented approach to handling runtime errors ◦ Can also handle runtime errors.

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

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.
Errors and Exceptions The objectives of this chapter are: To understand the exception handling mechanism defined in Java To explain the difference between.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Review Linked list: Doubly linked list, insertback, insertbefore Remove Search.
1 Handling Exceptions COSC 1567 C++ Programming Lecture 11.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 16 Exception Handling.
 2006 Pearson Education, Inc. All rights reserved. Exception Handling in C++ CS-2303, C-Term Exception Handling in C++ CS-2303 System Programming.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Exception Handling: A Deeper.
Error Handling with Exceptions Concepts C and other earlier languages often had multiple error-handling schemes, and these were generally established.
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.
C++ Exception Handling
1 CSC241: Object Oriented Programming Lecture No 28.
Exception Handling Introduction Exception handling is a mechanism to handle exceptions. Exceptions are error like situations. It is difficult to decide.
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
© 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 
Exception Handling An Exception is an indication of a problem that occurs during a program’s execution. Exception handling enables the programmer to create.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
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.
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
CPSC 252 Exception Handling Page 1 Exceptions and exception handling Client programmers can make errors using a class attempting to dequeue an item from.
Object Oriented Programming
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
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?? }
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
1 CSC241: Object Oriented Programming Lecture No 27.
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
Slides Credit Umair Javed LUMS Web Application Development.
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.
VB.Net - Exceptions Copyright © Martin Schray
Introduction to Exception Handling and Defensive Programming.
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.
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.
Exception Handling Programmers must deal with errors and exceptional situations: User input errors Device errors Empty disk space, no memory Component.
CS212: Object Oriented Analysis and Design Lecture 20: Exception Handling-II.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Exceptions and Assertions Chapter 15 – CSCI 1302.
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.
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.
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
JavaScript and Ajax (Control Structures) Week 4 Web site:
Defensive Programming. Good programming practices that protect you from your own programming mistakes, as well as those of others – Assertions – Parameter.
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.
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
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 in C++
C++ Exceptions.
Chapter 16 Exception Handling
CS212: Object Oriented Analysis and Design
Exceptions C++ Interlude 3
Chapter 14: Exception Handling
Exception Handling.
Exceptions for safe programming.
CMSC 202 Exceptions.
Presentation transcript:

Jerry Lebowitz

Topics

 Provides a facility for a systematic object oriented approach to handling runtime errors ◦ Can also handle runtime errors generated by C++ classes  One piece of code “throws” an exception that is “caught” by another piece of calling code that is willing to “handle” the error  Exceptions do not require parameters and are therefore suitable for use with constructors and operators

 Not all exceptions can be handled by a program ◦ Inadvertently overwriting of memory is handled by the operating system  Terminates the application ◦ Exceptions may also be defined as situations in which undesirable results happen

 One way to handle errors in functions is to return a signal or flag ◦ Function return value ◦ Some I/O functions (like fopen (opening a file)) return a “–1” when an error occurs  An “if” statement is required after each function invocation to check for an error errorCode = myFunction(...); if (errorCode != 0) Do something……...

 It is difficult for some functions (like finding the maximum or minimum of two numbers) to return a meaningful error value ◦ “-1” may be the maximum or minimum

 A great debugging tool  When an expression fails to meet a criteria (could be input data), a window pops up, execution stops with an error message along with an awful sound  The line number where the expression fails appears in a window  This failure is highly visible

 Not a good technique of running production programs since one should write a more friendly error handling routine  If the #NDEBUG appears before the include of or the asserts are ignored  (See examples: assert1.cpp and assert2.cpp)

 Exceptions ◦ Unpredictable result that requires special handling  Exception Handler ◦ Code that is executed when a particular exception occurs  Throw ◦ To signal the fact that an exception has occurred  Catch ◦ To process a thrown exception

 The problem is more complex when using classes Question?  How can an application find out if an error occurred when an object is instantiated or destroyed? ◦ Remember that constructors and destructors are implicitly called  They cannot return a value

 A block of code explicitly prefixed by the keyword “try”  All the statements that might cause an error are enclosed within “try” blocks ◦ Enclosed by opening and closing braces ◦ Preceded by the “try” keyword ◦ Not all the code needs to be in a try block ◦ One can have many try blocks within a program

 Applications generally do not know which statement caused the exception ◦ If it wants to recover from the error, it may need to start over at the start of the try block  Exceptions can only be caught by code inside a try block

 The statement that causes an exception need not be located directly in the try block ◦ It can be in a function that was invoked within the try block ◦ It can be in a function invoked by a function within the try block  The try block can be established at the beginning of a program

 A block of code explicitly prefixed by the keyword “catch”  The code that handles the exception ◦ Enclosed by opening and closing braces ◦ Preceded by the keyword “catch”  The code inside a catch block is executed whenever an exception of the appropriate type is thrown inside a preceding try block

 Can only be defined immediately after a try block  When an exception is thrown ◦ An object is created ◦ Constructor is called  A catch block can optionally have parameters

 Generate an exception – throwing a exception  Control does not return to the location that caused the exception (throw point) after the exception is handled  Throwing an exception ◦ Act like “goto” statements ◦ Control goes to the exception handler (catch block) and then falls through to the code following the catch block

 Syntax ◦ throw expression; ◦ where expression can be a value or a variable of any data type ◦ Examples:  throw “invalid value”;  throw myInt;

 All the catch blocks used with a particular throw must immediately follow the try block  The process of throwing and catching an exception involves searching the call chain for a catch block  More than one catch block can follow a try block ◦ Control is transferred to the appropriate catch block depending on the exception

 Arguments are needed when more information is required about the exception  Typical information ◦ What value caused the error

 The group of catch blocks act sort of like a switch statement  When an exception is handled, control passes to the statement following all the catch blocks  Once caught, an exception is handled ◦ All other catch blocks become irrelevant ◦ Control can never fall into a catch block

 If an exception is thrown and no catch block is found, the program terminates with an error message unless there is a catch all handler ◦ catch (...)

try { … } catch (datatype1 identifier) // exception handler { … } catch (datatype2 identifier) // exception handler { … } catch (…) // exception handler { … }  (See example except1.cpp and expect2.cpp)

 In the following example, Stack is a class that handles exceptions  There are two error scenarios handled by the Stack class ◦ Too many objects put on the stack (exceeding the capacity of the stack (array)) ◦ Too many objects popped off the stack (obtaining invalid data)

 An exception class, called Range is created ◦ class Range { } - used to connect a throw statement with a catch block ◦ Empty class (not always the case)  When an error occurs, an exception is “thrown” using the keyword “throw” ◦ throw Range( )  Throws the exception  Invokes the implicit constructor for the Range class  The exception class name must include the class in which it is located (Range) ◦ It must immediately follow the try block

 When an exception is thrown ◦ A destructor is called automatically called for any object that was created by code up to the point in the try block

 An exception class can be part of a class ◦ Defined in the public section of the Stack class specification  The exception class name must include the class in which it is located (stack::Range) ◦ It must immediately follow the try block  There can be multiple exceptions within a program ◦ (See examples: except3.cpp and except4.cpp)

 Remember when an exception is thrown ◦ An object is created  An exception class can have its own data and member functions  Exceptions with the distance class ◦ (See examples: except5.cpp and except6.cpp)

 Missing catch block and (…) ◦ See examples: (except7.cpp and except8.cpp)  Processing after an exception is caught ◦ See example: except9.cpp

 Handled exceptions via a hierarchy of classes  what function ◦ Returns a string containing the exception object thrown by C++’s built-in exception classes

 The class “exception” is the base class of the classes designed to handle exceptions ◦ The exception class contains a function “what” that returns the appropriate error message ◦ A child class out_of_range handles string subscript out of range error ◦ A child class bad_alloc handles “new” operator errors

 Base class of the exception classes ◦ exception  Contained in the header file exception  Two subclasses of exception (defined in stdexcept ): ◦ logic_error includes subclasses:  invalid_argument: for use when illegal arguments are 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 includes subclasses:  overflow_error and underflow_error

 (See examples: except10.cpp through except14.cpp)