CS 2704 Object Oriented Software Design and Construction

Slides:



Advertisements
Similar presentations
Topics Introduction Types of Errors Exceptions Exception Handling
Advertisements

Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
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.
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
Starting Out with C++, 3 rd Edition 1 Chapter 16 – Exceptions, Templates, and the Standard Template Library (STL) Exceptions are used to signal errors.
Exceptions OO Software Design and Construction Computer Science Dept Va Tech January 2002 ©2002 McQuain WD & Keller BJ 1 Exceptions exceptiona program.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
. 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.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© Copyright Eliyahu Brutman Exceptions. © Copyright Eliyahu Brutman Exceptions and Design Patterns - 2 Introduction to Exception Handling Definition:
© 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.
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
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.
C++ Exceptions STL Vector. Example int Quotient (int numer, int denom} { if (denom != 0) return (numer/denom); else //What to do?? }
VB.Net - Exceptions Copyright © Martin Schray
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
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.
Exception Handling Outline 23.1 Introduction
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
EE4E. C++ Programming Lecture 6 Advanced Topics. Contents Introduction Introduction Exception handling in C++ Exception handling in C++  An object oriented.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
CHAPTER 18 C – C++ Section 1: Exceptions. Error Handling with Exceptions Forces you to defend yourself Separates error handling code from the source.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Eighth Lecture Exception Handling in Java
C ++ MULTIPLE CHOICE QUESTION
Exception handling.
Java Exceptions a quick review….
Exception Handling in C++
IS 0020 Program Design and Software Tools
Exceptions Error Handling and Recovery
Abstract Class Abstract Class is a class which contains atleast one Pure Virtual function in it. Abstract classes are used to provide an Interface for.
Jim Fawcett CSE687-OnLine – Object Oriented Design Summer 2017
Jim Fawcett CSE687 – Object Oriented Design Spring 2001
Generics, Exceptions and Undo Command
Exceptions David Rabinowitz.
Andy Wang Object Oriented Programming in C++ COP 3330
Jim Fawcett CSE687 – Object Oriented Design Spring 2015
Object-Oriented Programming (OOP) Lecture No. 45
Why exception handling in C++?
Part IX Fundamentals of C and C++ Programming Exception Handling
CS102 – Exceptions David Davenport Latest: May 2015
Object Oriented Programming COP3330 / CGS5409
ATS Application Programming: Java Programming
Exceptions with Functions
Andy Wang Object Oriented Programming in C++ COP 3330
Java Programming Language
Chapter 17 Templates and Exceptions Part 2
Chapter 12 Exception Handling and Text IO
Throwing and catching exceptions
Exceptions and Templates
Exceptions CSCE 121 J. Michael Moore
CSC 270 – Survey of Programming Languages
Exception Handling.
Exceptions, Templates, and the Standard Template Library (STL)
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
Chapter 12 Exception Handling and Text IO Part 1
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
Java Basics Exception Handling.
CMSC 202 Exceptions.
Exception Handling.
Exceptions.
Presentation transcript:

CS 2704 Object Oriented Software Design and Construction April 23, 2018 Differences with Java Exception handling in C++ and Java are very similar: Same try block structure with catch handlers Same throw statements Same stack unwinding model The are also several differences: No Throwable or Exception base classes, so you have to build your own exception hierarchy As a result, there are no common operations supported by all exceptions (i.e., they are not all printable, do not all contain a message, etc.) Can throw primitive types too No stack traces Exceptions are only created by throw statements, so most unexpected failures do not result in exceptions No finally blocks, so you must guard against unexpected exceptions that occur within your catch blocks Exceptions Computer Science Dept Va Tech January 2002 ©2002 McQuain WD & Keller BJ

Standard C++ Exceptions Exceptions thrown by standard C++ library code: Note that not every exception in C++ is derived from exception. (Thus the base exception type cannot be used to catch every exception in C++.) The base exception class declares a const char* what() method for output of an exception message. Computer Science Dept Va Tech January 2002 ©2002 McQuain WD & Keller BJ

Exceptions for Access Violations CS 2704 Object Oriented Software Design and Construction April 23, 2018 Exceptions for Access Violations The iterator for a DListT template may be modified to throw an exception when an invalid instance of it is dereferenced: class IllegalAccess {}; // exception object . . . class iterator { public: iterator() { Position = NULL; } T& operator*() { if ( Position == NULL ) throw IllegalAccess(); return Position->Element; } Now, dereferencing an invalid iterator will produce behavior consistent with pointers. Exceptions Computer Science Dept Va Tech January 2002 ©2002 McQuain WD & Keller BJ

Exceptions for Arithmetic Errors CS 2704 Object Oriented Software Design and Construction April 23, 2018 Exceptions for Arithmetic Errors Exceptions may also be used to mimic aborted hardware errors: #include <limits> using namespace std; class ArithmeticException {}; class NegativeRadicand : public ArithmeticException {}; class DivideByZero : public ArithmeticException {}; class ConvergenceFailure : public ArithmeticException {}; double SquareRoot(double A) throw(ArithmeticException) { if ( A < 0.0 ) throw NegativeRadicand(); int Iterations = 0; const int ITERATIONLIMIT = 100; double EPS_DBL = numeric_limits<double>::epsilon(); double MIN_DBL = numeric_limits<double>::min(); . . . Exceptions Computer Science Dept Va Tech January 2002 ©2002 McQuain WD & Keller BJ

Exceptions for Arithmetic Errors CS 2704 Object Oriented Software Design and Construction April 23, 2018 Exceptions for Arithmetic Errors . . . double Xk, Xkp1; Xkp1 = A; while ( fabs(Xkp1 * Xkp1 - A) > EPS_DBL ) { Xk = Xkp1; if ( fabs(Xk) < MIN_DBL ) throw DivideByZero(); else Xkp1 = Xk / 2.0 + A / (2.0 * Xk); Iterations++; if ( Iterations > ITERATIONLIMIT ) throw ConvergenceFailure(); } return Xkp1; Exceptions Computer Science Dept Va Tech January 2002 ©2002 McQuain WD & Keller BJ

Hierarchy of Exceptions CS 2704 Object Oriented Software Design and Construction April 23, 2018 Hierarchy of Exceptions Using an inheritance hierarchy of exception objects allows for flexible control in the handler: #include <limits> using namespace std; class ArithmeticException {}; class NegativeRadicand : public ArithmeticException {}; class DivideByZero : public ArithmeticException {}; class ConvergenceFailure : public ArithmeticException {}; Always catch object-valued exceptions by reference (preserves polymorphism, eliminates copy-construction) Omit parameter name to avoid compiler warnings about unused identifiers, if you do not refer to it in the handler try { root = SquareRoot(x); } catch ( NegativeRadicand& N ) { cout << "caught a NegativeRadicand" << endl; catch ( ArithmeticException& A ) { cout << "caught a general arithmetic exception" << endl; Exceptions Computer Science Dept Va Tech January 2002 ©2002 McQuain WD & Keller BJ

CS 2704 Object Oriented Software Design and Construction April 23, 2018 Exception safety Exception safety in a component means that it exhibits reasonable behavior when an exception is thrown during its execution This includes all the usual expectations for error-handling: Resources should not be leaked The program should remain in a well-defined state so that execution can continue For most components, it also includes the expectation that when an error is encountered, it is reported to the caller The hardest part: usually ensuring the program remains in a well-defined state Often, individual methods must either run to completion, or have no effect at all Operations can never throw an exception and leave object(s) in an invalid intermediate state—this includes exceptions thrown indirectly by calling lower-level operations! Exceptions Computer Science Dept Va Tech January 2002 ©2002 McQuain WD & Keller BJ