Lesson 16 Exceptions Lesson 14 -- Exceptions1. Murphy’s Law Anything that can go wrong will go wrong Lesson 14 -- Exceptions2.

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
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.
CS102--Object Oriented Programming
1 Chapter 17-2 Templates and Exceptions Dale/Weems.
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.
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
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.
 2006 Pearson Education, Inc. All rights reserved. Exception Handling in C++ CS-2303, C-Term Exception Handling in C++ CS-2303 System Programming.
CSE 332: C++ exceptions Overview of C++ Exceptions Normal program control flow is halted –At the point where an exception is thrown The program call stack.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)
SE-1020 Dr. Mark L. Hornick 1 Exceptions and Exception Handling.
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
Understand Error Handling Software Development Fundamentals LESSON 1.4.
© Copyright Eliyahu Brutman Exceptions. © Copyright Eliyahu Brutman Exceptions and Design Patterns - 2 Introduction to Exception Handling Definition:
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
Exceptions Objectives At the conclusion of this lesson, students should be able to Explain the need for exceptions Correctly write programs that use.
CPSC 252 Exception Handling Page 1 Exceptions and exception handling Client programmers can make errors using a class attempting to dequeue an item from.
1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about.
Object Oriented Programming
1 CSC241: Object Oriented Programming Lecture No 27.
Understanding Events and Exceptions Lesson 3. Objective Domain Matrix Skills/ConceptsMTA Exam Objectives Understand events and event handling Understand.
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.
Handling Exceptions in java. Exception handling blocks try { body-code } catch (exception-classname variable-name) { handler-code }
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Exceptions. Why exceptions? We often strive for writing portable reusable code; we are able to detect errors, however our code may be used for many different.
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.
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.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
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++
Exceptions Error Handling and Recovery
16 Exception Handling.
Chapter 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.
Exceptions.
CMSC202 Computer Science II for Majors Lecture 16 – Exceptions
EXCEPTION HANDLING IN C++
Java Programming Fifth Edition
Exceptions, Templates, and the Standard Template Library (STL)
Why exception handling in C++?
Part IX Fundamentals of C and C++ Programming Exception Handling
EXCEPTION HANDLING.
Exceptions C++ Interlude 3
Chapter 14: Exception Handling
Exceptions with Functions
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 Chapter 9 Edited by JJ.
Exceptions and Templates
Exception Handling.
Standard Version of Starting Out with C++, 4th Edition
Exceptions, Templates, and the Standard Template Library (STL)
Exceptions for safe programming.
CMSC 202 Exceptions.
Presentation transcript:

Lesson 16 Exceptions Lesson Exceptions1

Murphy’s Law Anything that can go wrong will go wrong Lesson Exceptions2

Exceptions Allow you to deal with the things that go wrong: Indicate that something unexpected has occurred or been detected Allow program to deal with the problem in a controlled manner Can be as simple or complex as program design requires Lesson Exceptions3

Exceptions -- Terminology Exception: object or value that signals an error Throw an exception: send a signal that an error has occurred Catch/Handle an exception: process the exception; interpret the signal Lesson Exceptions4

Exceptions – Keywords throw – followed by an argument, is used to throw an exception try – followed by a block { }, is used to invoke code that throws an exception catch – followed by a block { }, is used to detect and process exceptions thrown in preceding try block. Takes a parameter that matches the type thrown. Lesson Exceptions5

Exceptions – Flow of Control 1.A function that throws an exception is called from within a try block 2.If the function throws an exception, the function terminates and the try block is immediately exited. A catch block to process the exception is searched for in the source code immediately following the try block. 3.If a catch block is found that matches the exception thrown, it is executed. If no catch block that matches the exception is found, the program terminates. Lesson Exceptions6

Exceptions – Example(1) // function that throws an exception int totalDays(int days, int weeks) { if ((days 7)) throw "invalid number of days"; // the argument to throw is the // character string else return (7 * weeks + days); } Lesson Exceptions7

Exceptions – Example (2) try // block that calls function { totDays = totalDays(days, weeks); cout << "Total days: " << days; } catch (char *msg) // interpret // exception { cout << "Error: " << msg; } Lesson Exceptions8

Exceptions – How It Works 1.try block is entered. totalDays function is called 2.If first parameter is between 0 and 7, total number of days is returned and catch block is skipped over (no exception thrown) 3.If exception is thrown, function and try block are exited, catch blocks are scanned for the first one that matches the data type of the thrown exception. catch block executes Lesson Exceptions9

Exceptions – How It Works Lesson Exceptions10

What if no Exception is Thrown? Lesson Exceptions11

Exceptions -- Notes Predefined functions such as new may throw exceptions The value that is thrown does not need to be used in catch block. – in this case, no name is needed in catch parameter definition – catch block parameter definition does need the type of exception being caught Lesson Exceptions12

Exception Not Caught? An exception will not be caught if – it is thrown from outside of a try block – there is no catch block that matches the data type of the thrown exception If an exception is not caught, the program will terminate Lesson Exceptions13

Exceptions and Objects An exception class can be defined in a class and thrown as an exception by a member function An exception class may have: – no members: used only to signal an error – members: pass error data to catch block A class can have more than one exception class Lesson Exceptions14

What Happens After catch Block? Once an exception is thrown, the program cannot return to throw point. The function executing throw terminates (does not return), other calling functions in try block terminate, resulting in unwinding the stack If objects were created in the try block and an exception is thrown, they are destroyed. Lesson Exceptions15

Nested try Blocks try/catch blocks can occur within an enclosing try block Exceptions caught at an inner level can be passed up to a catch block at an outer level: catch ( ) {... throw; // pass exception up } // to next level Lesson Exceptions16

Lesson Exceptions17