Download presentation
Presentation is loading. Please wait.
Published byDarlene Summers Modified over 6 years ago
1
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
2
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
3
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
4
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
5
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 / 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
6
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
7
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.