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.

Slides:



Advertisements
Similar presentations
Detecting Bugs Using Assertions Ben Scribner. Defining the Problem  Bugs exist  Unexpected errors happen Hardware failures Loss of data Data may exist.
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 The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
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.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 16 Exception Handling.
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.
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.
Debugging Techniques1. 2 Introduction Bugs How to debug Using of debugger provided by the IDE Exception Handling Techniques.
Rossella Lau Lecture 9, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 9: Application with Exception Handling 
Exceptions Objectives At the conclusion of this lesson, students should be able to Explain the need for exceptions Correctly write programs that use.
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
CPSC 252 Exception Handling Page 1 Exceptions and exception handling Client programmers can make errors using a class attempting to dequeue an item from.
C++ Exceptions STL Vector. Example int Quotient (int numer, int denom} { if (denom != 0) return (numer/denom); else //What to do?? }
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
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 –
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.
11. EXCEPTION HANDLING Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
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.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students, continue; and break; statements.
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.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.
Exception handling.
Exception Handling in C++
Exceptions Error Handling and Recovery
Chapter 16 Exception Handling
Jim Fawcett CSE687-OnLine – Object Oriented Design Summer 2017
Exceptions.
CMSC202 Computer Science II for Majors Lecture 16 – Exceptions
Jim Fawcett CSE687 – Object Oriented Design Spring 2001
Andy Wang Object Oriented Programming in C++ COP 3330
Jim Fawcett CSE687 – Object Oriented Design Spring 2015
Why exception handling in C++?
Part IX Fundamentals of C and C++ Programming Exception Handling
EXCEPTION HANDLING.
Object Oriented Programming COP3330 / CGS5409
Chapter 14: Exception Handling
CNS 3260 C# .NET Software Development
Exception Handling Chapter 9.
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.
Abdulmotaleb El Saddik University of Ottawa
Exception Handling Chapter 9 Edited by JJ.
Exceptions CSCE 121 J. Michael Moore
Part B – Structured Exception Handling
CMPT 102 Introduction to Scientific Computer Programming
Exception Handling.
Exceptions 1 CMSC 202.
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Object-Oriented Programming Using C++ Second Edition
Department of Computer and Information Science, School of Science, IUPUI Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI
Lecture 9.
Exceptions for safe programming.
CMSC 202 Exceptions.
Exception Handling.
ENERGY 211 / CME 211 Lecture 24 November 14, 2008.
Lab 1 Exception Handling.
CMSC 202 Lesson 20 Exceptions 1.
Presentation transcript:

Introduction to C++ computers and programming CMPT 129 © Janice Regan, CMPT 129 2017

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 such as Failure to allocate memory Dividing by 0 The programmer can also generate exceptions to deal with possible serious conditions/errors in his program © Janice Regan, CMPT 129 2017

Throwing Exceptions When exceptions happen we say that an exception is thrown When an exception is thrown by our program we should have error management code in our program to catch the exception and process the error condition Uncaught exceptions can lead to unexpected program terminations that can be difficult to localize © Janice Regan, CMPT 129 2017

When to use exceptions We already know how to use decision statements (ifs) to test if a dangerous situation or error has occurred. We know how to manage the errors so the user of the program receives useful / needed information However, when we use decision statements to manage errors in functions we often lose important information on when/where the error occurred ( which function called the function that failed … ). We can use exceptions to improve this © Janice Regan, CMPT 129 2017

Returning Error information from functions (1) When an error condition is detected by a decision statement in a function We can pass an error condition back to the calling function Usually this means a “special” value of the of the variable being returned -1 <= sin(x) <= x, so -99 could be chosen as a “special” value to indicate an error in sin(x) -∞ <= 2x <= ∞, so there is no “special” value to indicate an error in 2x. How can we pass back an error value in this case. Answer: use exceptions © Janice Regan, CMPT 129 2017

Using EXCEPTIONs To use exceptions we use two new C++ keyword try { // code to check catch ( exceptionParameter ) // code to manage errors } © Janice Regan, CMPT 129 2017

Explaining using exceptions The try block is the code between the { } following the keyword try The catch block is the code between the { } following the keyword catch The try block will execute one statement at a time. When one of the statements in the try block throws an exception the next statement executed will be at the start of the catch block, When the catch block finishes, then the code following the end of the catch block will execute © Janice Regan, CMPT 129 2017

Exceptions and functions To throw an exception you do not need a try or catch block. To catch an exception you need both a try and a catch block Assume the main function calls function B which calls function C In function C an exception is throw. C may not need a try or catch block. The exception may be caught in main function or function B If the exception is caught in function B it can be rethrown so it can also be caught in main The serious errors that cause termination of the program should be caught an processed in main © Janice Regan, CMPT 129 2017

Throwing an exception An exception is usually thrown in place of printing an error message and taking an action like terminating the program inside the function When a function throws an exception it then terminates and passes execution to its calling function Assume the following exception is thrown in function myPrint if ( myStream.fail() ) { throw “ERROR: cannot read input \n “; } © Janice Regan, CMPT 129 2017

Catching an exception Assume the main function called function myCalc which called function myPrint. An exception was thrown in myPrint (previous slide) In myCalc or main the exception can be caught and processed as follows try { myPrint (x, y, z); } catch (char* exceptionString) { // prints ERROR: cannot read input cout << exceptionString; } © Janice Regan, CMPT 129 2017

Why rethrow EXCEPTIONS In our example main calls function myCalc calls function myPrint Some types of exceptions generated in myPrint may be processed in myPrint, others in myCalc, and others in main A catch catches all exceptions that of a particular type (string, code, C++ exception) Exceptions to be processed in main that are caught in myCalc (when exceptions to be processed in myCalc are caught) must be rethrown by myCalc © Janice Regan, CMPT 129 2017

Catching / rethrowing an exception A function can take an action like printing a message before passing the exception to the function that called it myCalc can indicate that it called myPrint before sending the exception to the main program catch (const char* exceptionString) { cerr << “in this function MyCalc" << “ MyPrint was called” << endl; throw exceptionString; } © Janice Regan, CMPT 129 2017

You Can also send eRROR codes So far we have sent the string containing the error message in the exception handler An integer error code can also be sent if ( !(mystream >> myVar) ) { throw 10; } © Janice Regan, CMPT 129 2017

Catching Error codes When an integer code is thrown it can be caught as shown below catch(int code) { cout << “number is not an integer”; } © Janice Regan, CMPT 129 2017

Catching C++ generated exceptions When C++ throws an exception it can be caught as shown below. C++ throws exceptions for serious errors that would require the program be terminated Examples include, failing to allocate memory and dividing by 0 catch(exception& e) { cout << e.what(); //prints message } © Janice Regan, CMPT 129 2017

MULTIPLE EXCEPTIONS Inside one try block it is possible that multiple exceptions can be thrown. Some of the exceptions may send strings to be caught Some of the exceptions may send codes to be caught Each of the possible types of exception need to be processed in a separate catch block Exceptions that throw different codes will be processed in one catch block Exceptions that throw different strings will be processed in another block Exceptions thrown by the system will be processed in other blocks © Janice Regan, CMPT 129 2017

Using multiple catch blocks try { // code generating many different exceptions } catch(int code) // code processing exceptions that generate error codes catch(char *msg) // code processing exceptions than generate messages } catch( exception& e) // code processing C++ generated exceptions © Janice Regan, CMPT 129 2017

MULTIPLE EXCEPTIONS Inside one catch block it is possible that multiple different exceptions of the same type can be caught. Each of the possible different exceptions need to be processed in the catch block Assume function A calls function B calls function C Some types of exceptions generated in C may be processed in C, others in B, and others in A A catch catches all exceptions that of a particular type Exceptions to be processed in A that are caught in B must be rethrown by B © Janice Regan, CMPT 129 2017

Catching Multiple Error codes The catch block for integer codes can catch and process any number of different codes catch(int code) { if(code == 10) cout << “number is not an integer”; } //process any other codes to be managed in // this functions as else ifs, // any code may be re thrown after processing else throw code; //these codes need to be processed in calling function //no action needs to be tacken in this function © Janice Regan, CMPT 129 2017

Catching Multiple error messages The catch block for string messages can catch and process any number of different codes catch(char* msg) { if(msg == “number in file not in range”;) cout << “range is 0< x < 23”; cout << endl << msg; throw msg; } //process any other codes in else ifs // codes will only be re thrown if the exception // needs to be passed to the calling program © Janice Regan, CMPT 129 2017

Correcting exception conditions In some cases we were able to correct errors For example clearing the stream and rereading after attempting to read an integer and not being able to read any numbers We can also use try catch to recover in these cases For example our input loop for rereading data can be replaced by a try and catch inside a loop that repeats up to the maximum number of retries (see posted example) © Janice Regan, CMPT 129 2017