Date post: | 19-Jan-2016 |
Category: |
Documents |
Upload: | clementine-hart |
View: | 219 times |
Download: | 0 times |
Exception Handling
“A well behaved program never abnormally terminates. All errors or problems should be handled by the program”
Exception: A problem that occurs that is outside the normal expected behavior of the system. Example: Divide by zero
Exception Handling
• Most exceptions are infrequent or should not happen at all.
• The code for the exception should be separated from the main body to avoid confusing maintenance activities.
• Harkins back to the day of the “ go to “ when the execution path of computer programs was less well behaved.
Exception Handling
• Exception handling best used for situations that would normally cause the program to abnormally terminate or produce unpredictable results: – Divide by Zero– Array subscript out of bounds– Register or number overflow or
underflow– Stack overflow or underflow– Abnormal operation– Parity error
Simple Example#include <iostream>
using namespace std;
int main()
{
int cookies, people;
double cpp;
try {
cout << "Enter number of people: ";
cin >> people;
cout << "Enter number of cookies: ";
cin >> cookies;
if (cookies == 0) throw people;
else if (cookies < 0)
throw static_cast<double>(people);
cpp =okies/static_cast<double>(people);
cout << cookies << " cookies.\n"
<< people << " people.\n"
<< "You have " << cpp
<< " cookies per person.\n";
}
catch(int e)
{
cout << e << " people, and no cookies!\nGo buy some cookies!\n";
}
catch(double t)
{
cout << "Second catch block type double -- do we reach it?\n";
}
cout << "End of program.\n"; return 0; }
}
try Block
• The basic handling of an exception consists of the try-throw-catch trio.
• Basic try blocktry { // Some code goes here // Possibly throw an exception // More regular code can go here. }
try Block
• The try block is a method of grouping code together that could be associated with one or more exceptions and note affecting the rest of the program.
• If an exception is detected, the alternative code is executed while the execution of the normal code within the try block is halted.
• The code after the try block is not affected. • If an exception occurs within a try block, the
try block ends and the program attempts to match the exception with one of the catch handlers.
• If match found, code in catch is executed • Only one catch block will be executed • Execution resumes after last catch block
Example try-catch
try
{
if(Array_Index > 100)
throw Array_Index;
}
catch ( int e)
{
cout << e<< “ is greater than 100”<<
<< “resetting index to 100”\n;
Array_Index = 100;
}
Overuse of Exceptions
• Exceptions alter flow of control– Similar to old "goto" construct– "Unrestricted" flow of control
• Should be used sparingly• Good rule:– If desire a "throw": consider how to
write program without throw– Ask yourself how often you expect
to encounter this error– Errors caused by bad user input is
usually not a reason to write an exception to handle it
– If alternative reasonable do it