Part IX Fundamentals of C and C++ Programming Exception Handling

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 Errors can be dealt with at place error occurs –Easy to see if proper error checking implemented –Harder to read application.
Lecture 9. 2 Exception  An exception is a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring.
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.
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Exception Handling Handling an unexpected behaviour Unit - 08.
 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,
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.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution.
CSI 3120, Exception handling, page 1 Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8 th ed., 2007 Dr. Nathalie.
CS Advanced C++ Exception Handling Topic #5.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 14 - Exception Handling Outline 14.1Introduction 14.2When Exception Handling Should Be Used 14.3Other.
© 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.
1 Chapter 14 - Exception Handling Outline 14.1Introduction 14.2When Exception Handling Should Be Used 14.3Other Error Handling Techniques 14.4The Basics.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 13 - Exception Handling Outline 13.1Introduction 13.2When Exception Handling Should Be Used.
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 –
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.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 14 - Exception Handling Outline 14.1Introduction 14.2When Exception Handling Should Be Used 14.3Other.
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 Outline 23.1 Introduction
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
Exception Handling How to handle the runtime errors.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.
Appendix H Exception Handling: A Deeper Look
Exception handling.
IS 0020 Program Design and Software Tools
Exceptions Error Handling and Recovery
16 Exception Handling.
Exceptions Exceptions are used to signal that an unexpected event has happened in a program C++ will generate exceptions for some errors in the program.
EXCEPTION HANDLING IN C++
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 10 – Exception Handling
Pointers & Arrays.
Why exception handling in C++?
EXCEPTION HANDLING.
Chapter 14: Exception Handling
Exceptions with Functions
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 17 Templates and Exceptions Part 2
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Exception Handling and Reading / Writing Files
Advanced C++ Exception Handling
Exceptions and Templates
Exception Handling.
Exceptions 1 CMSC 202.
Standard Version of Starting Out with C++, 4th Edition
Lecture 11 Objectives Learn what an exception is.
Exceptions, Templates, and the Standard Template Library (STL)
Pointers & Arrays.
Exception and Event Handling
Object-Oriented Programming (OOP) Lecture No. 44
Department of Computer and Information Science, School of Science, IUPUI Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI
Lecture 9.
Java Programming: From Problem Analysis to Program Design, 4e
CMSC 202 Lesson 20 Exceptions 1.
Presentation transcript:

Part IX Fundamentals of C and C++ Programming Exception Handling EEL 3801 Part IX Fundamentals of C and C++ Programming Exception Handling

Exception Handling Used in situations when: Errors can potentially occur and the system can recover from them. Errors are synchronous in nature e.g., memory exhaustion division by zero arithmetic overflow out of bounds array subscript Errors will be dealt with in a different part of the program.

Exception Handling Not used in situations when: Errors are asynchronous in nature (e.g., network messages, disk I/O errors, mouse click errors). Interrupt processing are a better choice of techniques for these types of errors.

Exception Handling in C++ Tries a block of code that may contain exceptions Throws an exception when one is detected. Catches the exception and handles it. Thus, there are three concepts: the try block the throwing of the exceptions the block that catches or handles the exception

The try Block A block which includes the code that may generate an error (an exception). try { .. .. } Can be followed by one or more catch blocks which handle the exception

The try Block Control of the program passes from the statements in the try block, to the appropriate catch block. The throw point may be deeply nested within the try block Functions called from the try block, directly or indirectly, could test for the presence of the error.

The throw Point Used to indicate that an exception has occurred. It is called “throwing an exception” Throw normally specifies an operand: can be of any type can be an object (called the exception object) Will be caught by closest exception handler.

The catch Blocks Contain the exception handler. These know what to do with the exception - typically print out that a type of error has occurred. catch( ) { . . . }

Catching Exceptions catch blocks are typically located right after the try block that could throw the exception. The exception will be “caught” by the closest exception handler that specifies the proper type.

Exception Handling Procedure When exception identified in the try block control passes from the try block to the closest catch block that meets the type criteria. The code in correct catch block is executed. Control passes beyond the last catch block if there are no exceptions. An exception not caught will cause program to terminate prematurely.

Exception Handling Procedure Exception Handlers searched in order for an appropriate match. If more than 1 matches are appropriate, then the physically closest to the try block with threw the exception. The programmer determines the order of the catch blocks. Once an exception is thrown, control cannot return to the throw point.

Exception Handling - Example #include <iostream.h> class DivideByZeroError() { public: DivideByZeroError() : message(“Divide by Zero”) {}; void printMessage() const {cout << message ; } private: const char *message; This is called the error class.

Exception Handling - Example float quotient(int num1, int num2) { if (num2 == 0) throw DivideByZeroError(); return (float) num1/num2; } Throws an exception to the error class DivideByZeroError

Exception Handling - Example main() { cout << “Enter 2 integers”; int num1, num2. cin >> num1 >> num2; try { float result = quotient(num1, num2); cout << “the quotient is” << result; } . . . // continued on next slide

Exception Handling - Example catch (DivideByZeroError error) { cout << “ERROR:” error.printMessage() cout << endl; return 1; } return 0; ) An object of class DivideByZeroError is instantiated and named error.

Example Discussion Note that the detection element, quotient is embedded indirectly into the try block. Note that the error message contained in the exception class is printed out. The program control skips the catch block if an exception is not thrown. Several catch blocks can be placed in sequence after the try block.