Exception Handling
Exception Handling
CLO-3, PLO-2, C4
1
Exceptions
Exception Syntax
Imagine an application that creates and interacts with objects of
a certain class. Ordinarily the application’s calls to the class
member functions cause no problems. Sometimes, however, the
application makes a mistake, causing an error to be detected in a member
function. This member function then informs the application that an error has
occurred. When exceptions are used, this is called throwing an exception. In the
application we install a separate section of code to handle the error.
Exceptions
(Text book)
Exceptions
throw,
catch, and
try.
Exceptions
try block
catch block
throw
Sequence of Events
Let’s summarize the sequence of events when an exception
occurs:
1. Code is executing normally outside a try block.
2. Control enters the try block.
3. A statement in the try block causes an error in a
member
function.
4. The member function throws an exception.
5. Control transfers to the exception handler (catch block)
Exceptions
Example
main ()
{
int age;
cout<<"Enter your age in years"<<endl;
cin>>age; Output
cout<< " you are " <<age<<" years old" Enter your age in years
<<endl; 44
you are 44 years old
}
Exceptions
Example
main () Output-1
{ Enter your age in years
try 33
{ you are 33 years old
int age;
cout<<"Enter your age in years"<<endl; Output-2
cin>>age; Enter your age in years
if(age>0) { cout<< " you are " <<age<<" years old" <<endl; } -25
else { Operation aborted - invalid value.
throw(age); Age value cannt be: -25
}
} Output-3
catch (int age)
Enter your age in years
{ 0
cout << "Operation aborted - invalid value. \n"; Operation aborted - invalid value.
cout << "Age value cannt be: " << age; Age value cannt be: 0
}
}
xample Multiple exceptions
Exceptions
main () { Output
try { Enter your age in years
int age, xp; 24
cout<<"Enter your age in years"<<endl; cin>>age; you are 24 years old
if(age>0) { cout<< " you are " <<age<<" years old" <<endl; }
else if(age==0)
{ Output
throw(55); Enter your age in years
} 0
else if(age<0) Integer Exception, Ex value is:55
{
throw(3.5f);
}
}
Output
catch(int j) Enter your age in years
{ cout<<"Integer Exception, Ex value is:"<<j<<endl; -77
} Float Exception, Ex value is: 3.5
catch(float m) { cout<<"Float Exception, Ex value is: "<<m<<endl; }
catch(char k) { cout<<"Character Exception, Ex value is: "<<k; }
catch(...) { cout << "Exception: Unknown"; }
}
Exceptions
Example
main () {
try {
int age, xp; double p;
cout<<"Enter your age in years"<<endl; cin>>age; Output
if(age>0) { cout<< " you are " <<age<<" years old" <<endl; }
else if(age==0)
{ Enter your age in years
throw(p); 0
} Exception: Unknown
else if(age<0)
{
throw(3.5f);
}
}
catch(int j)
{ cout<<"Integer Exception, Ex value is:"<<j<<endl;
}
catch(float m) { cout<<"Float Exception, Ex value is: "<<m<<endl; }
catch(char k) { cout<<"Character Exception, Ex value is: "<<k; }
catch(...) { cout << "Exception: Unknown"; }
}
Exceptions
Analysis of Exception Handling
class hierarchy
Source: https://www.tutorialspoint.com/
Exceptions
Exception & Description
1 std::exception
An exception and parent class of all the standard C++ exceptions.
2 std::bad_alloc
This can be thrown by new.
3 std::bad_cast
This can be thrown by dynamic_cast.
4 std::bad_exception
This is useful device to handle unexpected exceptions in a C++
program.
5 std::bad_typeid
This can be thrown by typeid.
6 std::logic_error
An exception that theoretically can be detected by reading the code.
7 std::domain_error
This is an exception thrown when a mathematically invalid domain is
used.
8 std::invalid_argument
This is thrown due to invalid arguments.
Exceptions
9 std::length_error
This is thrown when a too big std::string is created.
10 std::out_of_range
This can be thrown by the 'at' method, for example a std::vector and
std::bitset<>::operator[]().
11 std::runtime_error
An exception that theoretically cannot be detected by reading the code.
12 std::overflow_error
This is thrown if a mathematical overflow occurs.
13 std::range_error
This is occurred when you try to store a value which is out of range.
14 std::underflow_error
This is thrown if a mathematical underflow occurs.
Source: https://www.tutorialspoint.com/