Upload
godwin-lambert
View
218
Download
1
Embed Size (px)
Citation preview
C++ ExceptionsSTL 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
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)
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.
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.
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
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
Example#include <stdexcept>#include <iostream>using namespace std;
class DivideByZeroException: public runtime_error{public:
DivideByZeroException():runtime_error("attempted to divide by zero\n")
{}};
double quotient(int num, int denom){
if (denom==0)throw DivideByZeroException();
return double(num/denom);}
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;}
}}
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 . . .
class logic_error : public exception { public: logic_error(const string& message); };
User defined exception class (2)
class TableException: public logic_error
{
public:
TableException(const string& message=""):
logic_error(message.c_str())
{}
};
Example Usage
try{
if (!found)throw TableException("Not in Table. Cannot remove\
n");}catch (TableException e){
cout << e.what();}
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 . . .