ENERGY 211 / CME 211 Lecture 24 November 14, 2008.

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

C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: 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.
 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,
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.
Lesson 16 Exceptions Lesson Exceptions1. Murphy’s Law Anything that can go wrong will go wrong Lesson Exceptions2.
Jerry Lebowitz. Topics  Provides a facility for a systematic object oriented approach to handling runtime errors ◦ Can also handle runtime errors.
C++ Exception Handling
Exception Handling Introduction Exception handling is a mechanism to handle exceptions. Exceptions are error like situations. It is difficult to decide.
CPSC 252 Exception Handling Page 1 Exceptions and exception handling Client programmers can make errors using a class attempting to dequeue an item from.
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
CIS 270—Application Development II Chapter 13—Exception Handling.
 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
VB.Net - Exceptions Copyright © Martin Schray
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
CSC 270 – Survey of Programming Languages C++ Lecture 6 – Exceptions.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
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.
CS212: Object Oriented Analysis and Design Lecture 20: Exception Handling-II.
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.
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
Exception Handling How to handle the runtime errors.
CHAPTER 18 C – C++ Section 1: Exceptions. Error Handling with Exceptions Forces you to defend yourself Separates error handling code from the source.
Lecture 18B Exception Handling and Richard Gesick.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
C ++ MULTIPLE CHOICE QUESTION
Exception handling.
Exception Handling in C++
C++ Exceptions.
IS 0020 Program Design and Software Tools
Exceptions Error Handling and Recovery
16 Exception Handling.
Jim Fawcett CSE687-OnLine – Object Oriented Design Summer 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.
EXCEPTION HANDLING IN C++
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
Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
Exceptions C++ Interlude 3
Chapter 14: Exception Handling
Exception Handling and
Exceptions with Functions
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 17 Templates and Exceptions Part 2
Chapter 12 Exception Handling and Text IO
Exception Handling Chapter 9 Edited by JJ.
Exception Handling In Text: Chapter 14.
Data Abstraction: The Walls
CSC 270 – Survey of Programming Languages
Programming in C# CHAPTER - 7
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
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Exceptions for safe programming.
Exceptions.
Presentation transcript:

ENERGY 211 / CME 211 Lecture 24 November 14, 2008

Exceptions In C, standard library functions indicate errors by returning appropriate numeric values Retrieving useful information about the error is cumbersome So is recovering from errors that occur several function calls deep C++ allows throwing exceptions to facilitate error reporting and recovery

Throwing Exceptions Use the throw keyword: throw object; The thrown object can be any object that can be copied So far we have used an object of type std::exception Can create your own exception class that contains information about the error leading to the exception

Consequences throw statement diverts execution outside smallest enclosing try block If no try block inside current function, calling function is checked, and so on Variables in each scope de-allocated Dynamically allocated memory is not de-allocated, so don't lose track of it! Once an error is detected, perform any necessary cleanup before throwing

Catching Exceptions Once a try block is found, execution proceeds with following catch blocks Argument list of each catch block is checked against thrown object If a match is found, statements in catch block are executed Thrown object is a local variable within the catch block where it is declared catch(…) matches anything

Exception Do's and Don'ts DO throw an exception within a constructor if anything goes wrong DO NOT throw an exception within a destructor; might crash program! DO NOT let an exception go uncaught, or execution will terminate immediately DO NOT use throw specifications like type fname(args) throw(type); because they limit flexibility

std::runtime_error Instead of throwing an object of type std::exception, can use the derived class std::runtime_error Declared in <stdexcept> header Construct with your own error message: using namespace std; runtime_error ex("Idiot!"); throw ex; When caught: ex.what() is a const char array with the error message

Next Time Basics of Computer Organization What goes on under the hood Function calls Memory management Linking and loading