Traditional Error Handling

Similar documents
COMP322 - Introduction to C++ Lecture 10 - Overloading Operators and Exceptions

! Errors can be dealt with at place error occurs

G Programming Languages - Fall 2012

Introduction. Common examples of exceptions

05-01 Discussion Notes

CS11 Advanced C++ Fall Lecture 3

Exceptions Programming 1 C# Programming. Rob Miles

Exceptions. Exceptions. Exceptional Circumstances 11/25/2013

Lecture Material. Exceptions

Object-Oriented Principles and Practice / C++

G52CPP C++ Programming Lecture 16

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

CS313D: ADVANCED PROGRAMMING LANGUAGE

Exception Handling Pearson Education, Inc. All rights reserved.

Chapter 17 vector and Free Store

Chapter 17 vector and Free Store

Exceptions in Java

Chapter 5 Errors. Bjarne Stroustrup

Chapter 5 Errors. Hyunyoung Lee. Based on slides by Bjarne Stroustrup.

Exception Handling in C++

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

A Third Look At Java. Chapter Seventeen Modern Programming Languages, 2nd ed. 1

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

CS 3 Introduction to Software Engineering. 3: Exceptions

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

Exception Handling Introduction. Error-Prevention Tip 13.1 OBJECTIVES

Errors and Exceptions

What can go wrong in a Java program while running?

Errors. Lecture 6. Hartmut Kaiser hkaiser/fall_2011/csc1254.html

IS 0020 Program Design and Software Tools

Vector and Free Store (Pointers and Memory Allocation)

ECE 122. Engineering Problem Solving with Java

Lecture 10. To use try, throw, and catch Constructors and destructors Standard exception hierarchy new failures

CMSC 202. Exceptions

Exceptions. Gunnar Gotshalks EX 1

Object Oriented Programming

17.1 Handling Errors in a Program

CS11 Introduction to C++ Fall Lecture 6

Abstract. Vector. Overview. Building from the ground up. Building from the ground up 1/8/10. Chapter 17 vector and Free Store

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

JAC444 - Lecture 4. Segment 1 - Exception. Jordan Anastasiade Java Programming Language Course

C++ Crash Kurs. Exceptions. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

void fun() C::C() // ctor try try try : member( ) catch (const E& e) { catch (const E& e) { catch (const E& e) {

CS102 C++ Exception Handling & Namespaces

Operators. The Arrow Operator. The sizeof Operator

Pointers and Memory 1

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

The vector Class and Memory Management Chapter 17. Bjarne Stroustrup Lawrence "Pete" Petersen Walter Daugherity Fall 2007

vector and Free Store

Exceptions. Errors and Exceptions. Dealing with exceptions. What to do about errors and exceptions

Exceptions. What exceptional things might our programs run in to?

Programming Languages and Techniques (CIS120)

Chapter 17 vector and Free Store. Bjarne Stroustrup

Software Design and Analysis for Engineers

Exception Handling. Chapter 9

Programming II (CS300)

Exception Handling Pearson Education, Inc. All rights reserved.

Error Handling in C++

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

Exceptions and Design


CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

Object Oriented Programming Exception Handling

CMSC131. Exceptions and Exception Handling. When things go "wrong" in a program, what should happen.

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

15. C++ advanced (IV): Exceptions

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Programming II (CS300)

Exception handling. Aishy Amer. Lecture Outline: Introduction Types or errors or exceptions Conventional error processing Exception handling in C++

COM S 213 PRELIM EXAMINATION #2 April 26, 2001

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

COE318 Lecture Notes Week 10 (Nov 7, 2011)

Introduction to Linked Lists. Introduction to Recursion Search Algorithms CS 311 Data Structures and Algorithms

COMP 213. Advanced Object-oriented Programming. Lecture 17. Exceptions

The University of Melbourne Department of Computer Science and Software Engineering Software Design Semester 2, 2003

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

Lecture 14 Summary 3/9/2009. By the end of this lecture, you will be able to differentiate between errors, exceptions, and runtime exceptions.

Exceptions. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 15

CPSC 427: Object-Oriented Programming

C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15

Laboratorio di Tecnologie dell'informazione

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

Lecture 2, September 4

Contents. Error Handling Strategies (cont d) Error handling strategies: error code, assert(), throw-try-catch

Intermediate Programming, Spring 2017*

Today. Instance Method Dispatch. Instance Method Dispatch. Instance Method Dispatch 11/29/11. today. last time

Exception Safe Coding

Lecture 20. Java Exceptional Event Handling. Dr. Martin O Connor CA166

Supplemental Handout: Exceptions CS 1070, Spring 2012 Thursday, 23 Feb 2012

C++ without Classes. CMSC433, Fall 2001 Programming Language Technology and Paradigms. More C++ without Classes. Project 1. new/delete.

Lecture 07 Debugging Programs with GDB

Preconditions. CMSC 330: Organization of Programming Languages. Signaling Errors. Dealing with Errors

Programming Languages and Techniques (CIS120e)

OCaml Data CMSC 330: Organization of Programming Languages. User Defined Types. Variation: Shapes in Java

Designing Robust Classes

More loops Ch

A Whirlwind Tour of C++

Transcription:

Exception Handling 1 Traditional Error Handling We have already covered several mechanisms for handling errors in C++. We ll quickly review them here. Returning Error Values One classic approach involves returning a predefined error constant from a function on an error condition. For example, the C memory management function malloc() returns NULL for out of memory, and the C file I/O function fopen() returns NULL for unsuccessful open. However, users may not check for an error condition, either because they forget or because they don t even know that they need to do so. For example, the C function printf() returns the number of arguments successfully printed. However, you will probably never encounter code that does anything with the return value of printf. Additionally, choosing a predefined error value is not always possible. For example, if operator[] on an array or vector is out of bounds, there s no value that is guaranteed not to appear in the contents of the array. Global Error Condition Flag Standard C provides errono() and perror() functions to support a global C standard library error code. POSIX, the de-facto standard for how UNIX systems, defined a global integer value errno. However, just as with returning an error value, users may choose to ignore the error code. Error checking bulks up normal code, making it less efficient and harder to read. Exception Handling Exceptions are a mechanism for handling errors out of band, in separate code that obeys scopes and C++ code flow.

Exception Handling 2 class Vector // detects out of range errors as exceptions int sz; public: class Range ; // an excepton class, defined within class Vector int& operator[] (int i) if (0 <= i && i < sz) return p[i]; // within range, OK throw Range(); // exception -- throw a Range object ; operator[] throws an exception, when it detects an out of range subscript i Handling an Exception When an exception is thrown, the error must be caught or else the program will crash. If an exception is uncaught in a particular function, the call stack (the layers of functions calling other functions) is unwound until something that handles the exception is found. In this manner, you can almost imagine an exception to be an alternate means to return from a function.

Exception Handling 3 void foo(vector& v) int i; try bar(v); // try block may or may not cause an exception catch (Vector::Range) // if try causes an exception, then catch a Range object // do something about the exception void bar(vector& v) v[v.size() + 1]; // trigger range error! The above code throws Range up to bar() (no handler here), then foo(), which catches Range. Advantages Instead of terminating the program, we can write more robust, fault-tolerant code. Instead of returning a value representing error (e.g., 0 or NULL) that may not always be feasible if there is no acceptable "error value, we explicitly separate error handling code form "ordinary" code. Exception handling is also more readable and more amenable to automated code tools such as Visual Studio. Instead of returning a legal value and leaving the program in an illegal state, which lets the program runs, but may cause mysterious crashes later on, we force developers to get the program to run acceptably. Exceptions as Objects You can throw any valid value in C++: an int, a double, a bool, a class, etc. However, there are significant advantages to throwing objects. For example, we can pass data members as part of the exception information, such as error codes, a failure message, etc. Something else is nice about exceptions as objects: we can use inheritance for abstraction!

Exception Handling 4 class MathErr ; // Base class for a hierarchy of exceptions class Overflow : public MathErr ; class Underflow : public MathErr ; class ZeroDivide : public MathErr ; Now we can catch any of these exceptions: try /*... */ catch (Overflow) /*... */ catch (Underflow) /*... */ catch (MathErr) /*... */ try can catch various exceptions, distinguishing them by their class names What do you think happens if code in try throws a ZeroDivide exception? MathErr catches it, since it is the base class for ZeroDivide However, a function need not catch all exceptions. Any exception not caught get passed on up the call stack. Exceptions in Constructors Exceptions especially a good idea for constructors because constructors may fail, but cannot produce an error value. Exception handling lets failure be transmitted out of the constructor, e.g.: Vector::Vector(int size) if (size < 0 max < size) throw Size(); // max is member of Vector Code creating Vectors can catch Size errors:

Exception Handling 5 Vector* f(int i) Vector* p; try p = new Vector v(i); catch (Vector::Size) p = new Vector v(vector::max)); return p; //end of f() Standard Exceptions The Standard C++ Library supports a set of standard exceptions. The base language throws exceptions: i.e., new throws bad_alloc, STL container classes such as vector throw out_of_range, invalid_argument, etc. The base class for all of these exceptions is a class called exception. A function what() returns a character representation of an exception. Acknowledgement Much of this lecture series is from Lehigh s introductory computer science course.