Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 C++ Exceptions STL Vector

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

3 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

4 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)

5 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.

6 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.

7 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

8 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

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

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

11 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; }

12 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...

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

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

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

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


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

Similar presentations


Ads by Google