DOC PREVIEW
FSU COP 3330 - Exception Handling

This preview shows page 1-2-3 out of 10 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Exception Handling Fall 2008Exception HandlingSlide 3Slide 4Simple ExampleSlide 6try BlockSlide 8Example try-catchOveruse of ExceptionsException HandlingFall 2008 Dr. David A. [email protected] 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 zeroException 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 errorSimple 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 blockExample try-catchtry{ 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


View Full Document

FSU COP 3330 - Exception Handling

Download Exception Handling
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Exception Handling and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Exception Handling 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?