C++ Exceptions STL Vector. Example int Quotient (int numer, int denom} { if (denom != 0) return (numer/denom); else //What to do?? }

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Exception Handling Outline 13.1 Introduction 13.2 Exception-Handling Overview 13.3 Other.
Advertisements

Lecture 9. 2 Exception  An exception is a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring.
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.
1 Chapter 17-2 Templates and Exceptions Dale/Weems.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
 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.
Ch 11. Exception Handling Timothy Budd Oregon State University.
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.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
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.
Jerry Lebowitz. Topics  Provides a facility for a systematic object oriented approach to handling runtime errors ◦ Can also handle runtime errors.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
1 CS 204 Advance Programming Exception Handling in C++ Horton, pp. 239 – 247(the contents in the slides may be different than the book)
Rossella Lau Lecture 9, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 9: Application with Exception Handling 
Exception Handling An Exception is an indication of a problem that occurs during a program’s execution. Exception handling enables the programmer to create.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
Introduction to C++ Templates and Exceptions l C++ Function Templates l C++ Class Templates l Exception and Exception Handler.
Dale Roberts Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
CPSC 252 Exception Handling Page 1 Exceptions and exception handling Client programmers can make errors using a class attempting to dequeue an item from.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 13 - Exception Handling Outline 13.1Introduction 13.2When Exception Handling Should Be Used.
Handling ErrorstMyn1 Handling Errors Up to this point we haven't worried much about errors or exceptions. First, let's distinguish between errors and exceptions.
1 CSC241: Object Oriented Programming Lecture No 27.
Chapter 1 Introduction to C++: Name space: Defines an area in which names have scope. Can use the same name in different name spaces. Header file (file.
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.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Exceptions and Program Correctness based on the original work by Dr. Roger deBry Version 1.1.
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 Outline 23.1 Introduction
LECTURE LECTURE 14 Exception Handling Textbook p
1 Chapter 17 Templates and Exceptions Dale/Weems.
Friend classes Friend class methods Nested classes Throwing exceptions, try blocks and catch blocks Exception classes Runtime type identification (RTTI)
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
C++ Namespaces, Exceptions CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011.
Exception Handling How to handle the runtime errors.
EE4E. C++ Programming Lecture 6 Advanced Topics. Contents Introduction Introduction Exception handling in C++ Exception handling in C++  An object oriented.
Introduction to C++ Templates and Exceptions C++ Function Templates C++ Class Templates Exception and Exception Handler.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.
C ++ MULTIPLE CHOICE QUESTION
C++ Exceptions.
IS 0020 Program Design and Software Tools
Exceptions Error Handling and Recovery
Chapter 16 Exception Handling: A Deeper Look
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: A Deeper Look
Andy Wang Object Oriented Programming in C++ COP 3330
Why exception handling in C++?
Part IX Fundamentals of C and C++ Programming Exception Handling
EXCEPTION HANDLING.
Chapter 14: Exception Handling
Andy Wang Object Oriented Programming in C++ COP 3330
CS 204 Advance Programming Exception Handling in C++
Chapter 17 Templates and Exceptions Part 2
Throwing exceptions.
Throwing exceptions.
Exceptions 1 CMSC 202.
Object-Oriented Programming (OOP) Lecture No. 43
Object-Oriented Programming (OOP) Lecture No. 44
Department of Computer and Information Science, School of Science, IUPUI Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI
Lecture 9.
CMSC 202 Lesson 20 Exceptions 1.
Presentation transcript:

C++ Exceptions STL Vector

Example int Quotient (int numer, int denom} { if (denom != 0) return (numer/denom); else //What to do?? }

How to handle this problem? Options: Print an error message and halt the program Rewrite the function with a third parameter (bool) indicating success or failure Allow the function to have a precondition: Test for denom==0 before function is called Use C++ exception-handling mechanism

Definitions Exception – an unusual event that requires special processing Exception handler – a section of a program that is executed when a particular exception occurs Throw – to signal the fact that an exception has occurred Catch – to process a thrown exception (performed by an exception handler)

Exceptions When a section of code announces that an exception has occurred, the program should throw an exception and hope that another section of code (the exception handler) will catch the exception and process it.

The C++ exception class class exception { public: exception( ); exception(const char *const&); exception(const char *const&, int); exception(const exception&); exception& operator=(const exception&); virtual ~exception( ); virtual const char *what( ) const; }; Specifically, this base class is the root of the standard exception classes defined in. The C string value returned by what is left unspecified by the default constructor, but may be defined by the constructors for certain derived classes as an implementation-defined C string. None of the member functions throw any exceptions. The int parameter allows you to specify that no memory should be allocated. The value of the int is ignored.

Standard Exception Standard C++ Library Reference Defines several standard classes used for reporting exceptions. The classes form a derivation hierarchy all derived from class exception and include two general types of exceptions: logical errors and run- time errors. The logical errors are caused by programmer mistakes. They derive from the base class logic_error and include:exception domain_error invalid_argument length_error out_of_range

The run-time errors occur because of mistakes in either the library functions or in the run-time system. They derive from the base class runtime_error and include: overflow_error range_error underflow_error

Example #include using namespace std; class DivideByZeroException: public runtime_error { public: DivideByZeroException() :runtime_error("attempted to divide by zero\n"){} };

double quotient(int num, int denom) { if (denom==0) throw DivideByZeroException(); return double(num/denom); }

int main() { double result; int number1; int number2; for (int i=1; i<5; i++) { cout <<"Enter 2 integers\n"; cin >>number1; cin >> number2; try { result=quotient(number1, number2); cout <<"The quotient is " << result <<endl<<endl; } catch (DivideByZeroException e) { cout <<"Exception occurred: " <<e.what() << endl; }

Output of program Enter 2 integers 5 3 The quotient is 1 Enter 2 integers 9 0 Exception occurred: attempted to divide by zero Enter 2 integers 4 2 The quotient is 2 Enter 2 integers 10 5 The quotient is 2 Press any key to continue...

class logic_error : public exception { public: logic_error(const string& message); };

User defined exception class (2) class TableException: public logic_error { public: TableException(const string& message=""): logic_error(message.c_str()) {} };

Example Usage try { if (!found) throw TableException ("Not in Table. Cannot remove\n"); } catch (TableException e) { cout << e.what(); }

Execution Example Retrieve book information 2 - Delete book 3 - Print all books 4 - Exit the program Enter book isbn number 1492 Item has been deleted Retrieve book information 2 - Delete book 3 - Print all books 4 - Exit the program Enter book isbn number 1492 Table Exception: Item not found Retrieve book information 2 - Delete book 3 - Print all books 4 - Exit the program Thank for processing Press any key to continue...