Dr. David A. Gaitros Research Associate [email protected] 8/20/20091Dr. David A. Gaitros.
Exception Handling Fall 2008 Dr. David A. Gaitros [email protected].
-
Upload
clementine-hart -
Category
Documents
-
view
219 -
download
0
Transcript of Exception Handling Fall 2008 Dr. David A. Gaitros [email protected].
![Page 2: Exception Handling Fall 2008 Dr. David A. Gaitros dgaitros@admin.fsu.edu.](https://reader036.fdocuments.us/reader036/viewer/2022081512/5697c0261a28abf838cd5c63/html5/thumbnails/2.jpg)
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
![Page 3: Exception Handling Fall 2008 Dr. David A. Gaitros dgaitros@admin.fsu.edu.](https://reader036.fdocuments.us/reader036/viewer/2022081512/5697c0261a28abf838cd5c63/html5/thumbnails/3.jpg)
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.
![Page 4: Exception Handling Fall 2008 Dr. David A. Gaitros dgaitros@admin.fsu.edu.](https://reader036.fdocuments.us/reader036/viewer/2022081512/5697c0261a28abf838cd5c63/html5/thumbnails/4.jpg)
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
![Page 5: Exception Handling Fall 2008 Dr. David A. Gaitros dgaitros@admin.fsu.edu.](https://reader036.fdocuments.us/reader036/viewer/2022081512/5697c0261a28abf838cd5c63/html5/thumbnails/5.jpg)
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";
}
![Page 6: Exception Handling Fall 2008 Dr. David A. Gaitros dgaitros@admin.fsu.edu.](https://reader036.fdocuments.us/reader036/viewer/2022081512/5697c0261a28abf838cd5c63/html5/thumbnails/6.jpg)
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; }
}
![Page 7: Exception Handling Fall 2008 Dr. David A. Gaitros dgaitros@admin.fsu.edu.](https://reader036.fdocuments.us/reader036/viewer/2022081512/5697c0261a28abf838cd5c63/html5/thumbnails/7.jpg)
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. }
![Page 8: Exception Handling Fall 2008 Dr. David A. Gaitros dgaitros@admin.fsu.edu.](https://reader036.fdocuments.us/reader036/viewer/2022081512/5697c0261a28abf838cd5c63/html5/thumbnails/8.jpg)
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
![Page 9: Exception Handling Fall 2008 Dr. David A. Gaitros dgaitros@admin.fsu.edu.](https://reader036.fdocuments.us/reader036/viewer/2022081512/5697c0261a28abf838cd5c63/html5/thumbnails/9.jpg)
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;
}
![Page 10: Exception Handling Fall 2008 Dr. David A. Gaitros dgaitros@admin.fsu.edu.](https://reader036.fdocuments.us/reader036/viewer/2022081512/5697c0261a28abf838cd5c63/html5/thumbnails/10.jpg)
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