. Plab – Tirgul 10 Exceptions. Error handling in C Up to now we handled our errors in the “C way”: assert return codes global error variable ( errno and.

Slides:



Advertisements
Similar presentations
L7: Exceptions Problem Description Error Handling –terminate the program –return a value representing an error –return a legal value and leave the program.
Advertisements

Chapter 18 Vectors and Arrays
Chapter 18 Vectors and Arrays John Keyser’s Modification of Slides by Bjarne Stroustrup
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.
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.
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.
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.
Monday, Mar 24, 2003Kate Gregory with material from Deitel and Deitel Week 11 Exceptions.
C++ Exception Handling
1 CSC241: Object Oriented Programming Lecture No 28.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Exception Handling Introduction Exception handling is a mechanism to handle exceptions. Exceptions are error like situations. It is difficult to decide.
Pointers Revisited l What is variable address, name, value? l What is a pointer? l How is a pointer declared? l What is address-of (reference) and dereference.
© Copyright Eliyahu Brutman Exceptions. © Copyright Eliyahu Brutman Exceptions and Design Patterns - 2 Introduction to Exception Handling Definition:
Template class Wrapper { public: T* operator->() { return &myT; } private: T myT; }; int main() { Wrapper wThing; wThing- >Foo(); // calls Thing::Foo()...
Exceptions David Rabinowitz. March 3rd, 2004 Object Oriented Design Course 2 The Role of Exceptions Definition: a method succeeds if it terminates in.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
Plab – Tirgul 8 Exceptions. Error handling in C Up to now we handled our errors in the “C way”: assert return codes global error variable ( errno and.
OOP Spring 2007 – Recitation 81 Object Oriented Programming Spring 2007 Recitation 8.
SNU OOPSLA Lab. 14. Exception Handling © copyright 2001 SNU OOPSLA Lab.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 13 - Exception Handling Outline 13.1Introduction 13.2When Exception Handling Should Be Used.
CSE 332: C++ memory management idioms C++ Memory Management Idioms Idioms are reusable design techniques in a language –We’ll look at 4 important ones.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Exceptions Handling Exceptionally Sticky Problems.
Introduction to Exception Handling and Defensive Programming.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
C++ Memory Overview 4 major memory segments Key differences from Java
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.
CS212: Object Oriented Analysis and Design Lecture 20: Exception Handling-II.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
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.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
C++ Namespaces, Exceptions CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011.
CSC241 Object-Oriented Programming (OOP) Lecture No. 22.
EE4E. C++ Programming Lecture 6 Advanced Topics. Contents Introduction Introduction Exception handling in C++ Exception handling in C++  An object oriented.
CHAPTER 18 C – C++ Section 1: Exceptions. Error Handling with Exceptions Forces you to defend yourself Separates error handling code from the source.
Memory Management in Java Mr. Gerb Computer Science 4.
Pointers and References. Pointers & Memory 0x000x040x080x0B0x100x140x180x1B0x20.
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++
C++ Exceptions.
IS 0020 Program Design and Software Tools
CS212: Object Oriented Analysis and Design
CS 2704 Object Oriented Software Design and Construction
Overview 4 major memory segments Key differences from Java stack
Object-Oriented Programming (OOP) Lecture No. 45
Pointers Revisited What is variable address, name, value?
CMSC 202 Lesson 21 Exceptions II.
Chapter 14: Exception Handling
Overview 4 major memory segments Key differences from Java stack
Overview of Memory Layout in C++
Exceptions 2 CMSC 202.
Objects Managing a Resource
Pointers and References
Presentation transcript:

. Plab – Tirgul 10 Exceptions

Error handling in C Up to now we handled our errors in the “C way”: assert return codes global error variable ( errno and perror ) “The problem with C’s approach to error handling could be thought of as coupling—the user of a function must tie the error-handling code so closely to that function that it becomes too ungainly and awkward to use ”

Exceptions C++ introduces the use of exceptions to handle errors. You are already familiar with exceptions in Java. Unfortunately, in C++, due to the more complex memory model, the exceptions are far less simple .

Exceptions – why? u Exceptions are used in C++ for:  Handling errors in constructors.  Handling memory errors.  Math errors.

Throwing exception example If pop() from empty ChangesCollection is illegal we would like to throw an exception. const Change* ChangesCollection::pop() { if (isEmpty()) throw 0;... } In C++ anything can be thrown: basic types (int, double, etc.), pointers, objects, references.

Throwing exception const Change* ChangesCollection::pop() { if (isEmty()) throw EmptyCollectionE(__FILE__, __LINE__);... } It’s a good practice to define special exception classes (usually there will be a hierarchy of these): If an exception is not caught it will abort the program. Unlike Java you will get no info about the exception (such as where it was thrown, the call stack).

Catching an exception We use try and catch similarly to Java. Consider: try {... } catch (CollectionE& e) { e.printErrorMessage(); throw; // re-throw the exception } Note that catching by reference enables polymorphism.

Catching an exception cntd. We can catch several different exceptions at a time: try {... } catch (EmptyCollectionE& e) {... } catch (CollectionE& e) {... } catch (...) { // catch ANY exception... } Note the order of the catch clauses

Exception specification u Unlike Java, functions (methods) do not have to declare what exceptions they can throw:  int f(); // can throw any exception u Still, it’s a good style to declare the exceptions your functions may throw:  int f() throw (BadArg, int*); // f can throw only BadArg, int* u This function does not throw exceptions:  void g() throw();

Stack unwinding u When exception is thrown all automatic variable of the function on the stack are freed. u What about other resources, like:  open files  memory on heap FILE* f = fopen(filename, “r”); try {... } catch(...) { fclose(f); throw; } fclose(f); Note that in most C++ compilers there is no finally.

Stack unwinding cntd. u This solution is tedious if the resources are allocated in many different places. u A better solution is to wrap the resource in an object, which will be automatically deallocated:

Stack unwinding cntd. FilePtr f(filename, “r”); try {... // here f is used } catch(...) {... }  The file is closed by FilePtr ’s destructor, thus it will be closed regardless of the way we exit (normally or with exception).

Exceptions in constructor u Very convenient u If the object is constructed in (dynamic) heap and its constructor throws exception, the memory will be released automatically. u The destructor won’t be called. u Example: Circle’s constructor throws exception when provided with negative radius: try { c = new Circle(1, 2, -1); } catch(...) { // we should not delete c ! }

Exceptions in constructor u Example: Circle’s constructor throws exception when provided with negative radius: try { c = new Circle(1, 2, -1); } catch(...) { // we should not delete c ! }

Standard exceptions exception logic_errorruntime_error ios::failure invalid_argument out_of_range bad_alloc And others.

Read yourselves: auto_ptr  Similarly to the way we wrapped a FILE pointer, we can wrap any pointer, so that it is automatically deleted when we leave the scope.  The auto_ptr which is part of the standard library is a template class for this purpose. MyClass* f(...) { auto_ptr p(new MyClass());... return p.release(); }  If an exception occurs prior to return, the instance of MyClass will be automatically deleted by p ’s destructor. u After auto_ptr is copied (to another one) it points nowhere.