16
C++ Exceptions STL Vector

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

Embed Size (px)

Citation preview

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

C++ ExceptionsSTL Vector

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

Example

int Quotient (int numer, int denom}{

if (denom != 0)return (numer/denom);

else //What to do??}

Page 3: 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

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

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)

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

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.

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

The C++ exception classclass 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

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

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

Standard Exception<stdexcept>

Standard C++ Library Reference <stdexcept> 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:

• domain_error • invalid_argument • length_error • out_of_range

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

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

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

Example#include <stdexcept>#include <iostream>using namespace std;

class DivideByZeroException: public runtime_error{public:

DivideByZeroException():runtime_error("attempted to divide by zero\n")

{}};

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

double quotient(int num, int denom){

if (denom==0)throw DivideByZeroException();

return double(num/denom);}

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

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

}}

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

Output of programEnter 2 integers53The quotient is 1

Enter 2 integers90Exception occurred: attempted to divide by zero

Enter 2 integers42The quotient is 2

Enter 2 integers105The quotient is 2

Press any key to continue . . .

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

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

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

User defined exception class (2)

class TableException: public logic_error

{

public:

TableException(const string& message=""):

logic_error(message.c_str())

{}

};

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

Example Usage

try{

if (!found)throw TableException("Not in Table. Cannot remove\

n");}catch (TableException e){

cout << e.what();}

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

Execution Example+++++++++++++++++++++++++++++++++++++++++1 - Retrieve book information2 - Delete book3 - Print all books4 - Exit the program++++++++++++++++++++++++++++++++++++++++2Enter book isbn number1492Item has been deleted

+++++++++++++++++++++++++++++++++++++++++1 - Retrieve book information2 - Delete book3 - Print all books4 - Exit the program++++++++++++++++++++++++++++++++++++++++1Enter book isbn number1492Table Exception: Item not found+++++++++++++++++++++++++++++++++++++++++1 - Retrieve book information2 - Delete book3 - Print all books4 - Exit the program++++++++++++++++++++++++++++++++++++++++4Thank for processingPress any key to continue . . .