Andy Wang Object Oriented Programming in C++ COP 3330

Slides:



Advertisements
Similar presentations
Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
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.
Chapter 16 Exception Handling. What is Exception Handling? A method of handling errors that informs the user of the problem and prevents the program from.
Lecture 9. 2 Exception  An exception is a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring.
Exception Handling Introduction Try Throw Catch. Exception Handling Use exception handling to write programs that are –Clearer –More robust –More fault-tolerant.
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.
Review Linked list: Doubly linked list, insertback, insertbefore Remove Search.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 16 Exception Handling.
CSIS 123A Lecture 11 Exception Handling. Introduction  Typical approach to development:  Write programs assuming things go as planned  Get ‘core’ working.
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,
 2002 Prentice Hall, Inc. All rights reserved. Chapter 14 – Exception Handling Outline 14.1 Introduction 14.2 When Exception Handling Should Be Used 14.3.
Exception Handling. Introduction One benefit of C++ over C is its exception handling system. An exception is a situation in which a program has an unexpected.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution.
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
 2002 Prentice Hall. All rights reserved Exception-Handling Overview Exception handling –improves program clarity and modifiability by removing.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Exceptions Objectives At the conclusion of this lesson, students should be able to Explain the need for exceptions Correctly write programs that use.
Dale Roberts Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 9 : Exception Handling King Fahd University of Petroleum & Minerals College of Computer.
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.
C++ Exceptions STL Vector. Example int Quotient (int numer, int denom} { if (denom != 0) return (numer/denom); else //What to do?? }
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
1 CSC241: Object Oriented Programming Lecture No 27.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  Due to poor understanding of the problem and solution.
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.
Operator Overloading & Exception Handling TCP1201 OOPDS 1 Lecture 5 1.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
CSC 270 – Survey of Programming Languages C++ Lecture 6 – Exceptions.
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.
Exception Handling Fall 2008 Dr. David A. Gaitros
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
LECTURE LECTURE 14 Exception Handling Textbook p
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
Exception Handling How to handle the runtime errors.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.
Exception handling.
Exception Handling in C++
IS 0020 Program Design and Software Tools
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.
Jim Fawcett CSE687 – Object Oriented Design Spring 2001
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.
Object Oriented Programming COP3330 / CGS5409
Exception Handling and
Exceptions with Functions
Chapter 17 Templates and Exceptions Part 2
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
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.
CSC 270 – Survey of Programming Languages
Exception Handling.
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.
Exceptions for safe programming.
CMSC 202 Exceptions.
CMSC 202 Lesson 20 Exceptions 1.
Presentation transcript:

Andy Wang Object Oriented Programming in C++ COP 3330 Exception Handling Andy Wang Object Oriented Programming in C++ COP 3330

Exception Handling A type of error checking, available in many programming languages Exception Some sort of problem or error that occurs during a program’s execution Many error situations could occur besides those that we usually check for (usually related to things like user input) Exception handler A piece of code that resolves an exception situation Typical error check often intermixed with the tasks of a program (if statements, etc) Exception handlers are intended to be separate from the main tasks

Why? Intermixing program logic with the error-checking code can sometimes make programs hard to read, debug, etc. Many potential problems are very infrequent Exception handlers are separate from main tasks of a program Can improve clarity and modifiability Exception handling can improve a program’s fault tolerance

When? This does not mean that exception handlers should be used in all cases Sometimes conventional error checking is more appropriate Exception handling best for problems that occur infrequently Errors that will result in termination of the program Not for user input checking Good for setting up uniform techniques for error handling when many programmers and multiple modules are involved

How? Reserved words in C++: try throw, catch try blocks catch blocks Consists of keyword try and a block of code inside { } Encloses the statements that might cause exceptions catch blocks 1+ catch blocks follow a try block (also in { }) Each catch block is an exception handler A cache block has a single parameter (with type listed)

How? If an exception occurs in a try block The try block immediately ends Program attempts to match the exception to one of the catch handlers (based on type of item thrown) If a match is found, the code in the catch block executes Only one catch block will be matched, if any Program control resumes after the last catch block If no exception occur in a try block, the catch blocks are skipped A point where an exception occurs is the throw point Keyword throw used to throw an exception to be caught

How? In C++, there is a standard library with pre-built exception classes #include <exception> using std::exception; When a function intends to throw an exception back to the caller (not handled internally), it’s good to tell the caller what to expect via a throw list when declaring a function void someFunction throw (DivideByZero, OtherException); This can be used to limit the type of exceptions that a function is allowed to throw

How? The first function can throw no exceptions to the outside void someFunction1() throw(); // empty throw list void someFunction2(); // no throw list The first function can throw no exceptions to the outside The second can throw exceptions of any kind

Simple Example http://www.cs.fsu.edu/~myers/cop3330/examples/exc eptions/except.cpp

Main.cpp #include <iostream> using namespace std; int main() { int cookies, people; double cpp; try { cout << “Enter number of people: “; cin >> people; cout << “Enter number of cookies: “; cin >> cookies if (cookies == 0) throw people; if (cookies < 0) throw static_cast<double>(people);

Main.cpp cpp = cookies/static_cast<double>(people); cout << cookies << “ cookies.\n” << people << “ people.\n” << “You have “ << cpp << “ cookies per person.\n”; } catch(int e) { cout << e << “ people, and no cookies!\nGo buy some cookies!\n”; catch(double t) { cout << “Second catch block type double – do we reach it?\n”; cout << “End of program.\n”; return 0;

Negative Number Example http://www.cs.fsu.edu/~myers/savitch3c++/Ch18/18- 04.cpp

18-04.cpp #include <iostream> #include <string> using namespace std; class NegativeNumber { public: NegativeNumber() {} NegativeNumber(string m): message(m) {} string getMessage() const ( return message; } private: string message; }; class DivideByZero {};

18-04.cpp int main() { int pencils, erasers; double ppe; try { cout << “How many pencils do you have?\n”; cin >> pencils; if (pencils < 0) throw NegativeNumber(“pencils”); cout << “How many erasers do you have?\n”; cin >> erasers; if (erasers < 0) throw NegativeNumber(“erasers”); if (erasers != 0) ppe = pensils/static_cast<double>(erasers); else throw DivideByZero();

18-04.cpp cout << “Each eraser must last through “ << ppe << pencils.\n”; } catch(NegativeNumber e) { cout << “Cannot have a negative number of “ << e.getMessage() << endl; catch(DivideByZero) { cout << “Do not make any mistakes.\n”; cout << “End of program.\n”; return 0;

Safe Divide Example http://www.cs.fsu.edu/~myers/savitch3c++/Ch18/18- 05.cpp

18-05.cpp #include <iostream> #include <cstdlib> using namespace std; class DivideByZero {}; double safeDevide(int top, int bottom) throw (DivideByZero) { if (bottom == 0) throw DivideByZero(); return top/static_cast<double>(bottom); }

18-05.cpp int main() { int numerator, denominator; double quotient; cout << “Enter numerator:\n”; cin >> numerator; cout << “Enter denominator:\n”; cin >> denominator; try { quotient = safeDivide(numerator, denominator); } catch(DivideByZero) { cout << “Error: Division by zero!\n”; << “Program aborting.\n”; exit(0); cout << numerator << “/” << denominator << “ = “ << quotient << endl; return 0;