Exception Handling

Exception Handling - Lecture 10 Exception Handling...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 10 Exception Handling
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Exceptions Something unusual or incorrect When writing a program May first assume that nothing unusual or incorrect will happen Then add code to take care of the exceptional cases Exception handling Code to handle exception situations If an “error” is handled properly/correctly it no longer is an error.
Background image of page 2
A Toy Example Original code double sum, count, ave; cin >> sum >> count; ave = sum/count; //exception: divide-by-zero Handling a special case without exception handling if (count <=0) cout << “No number, No average!\n”; else {ave = sum/count; cout << “Average is: “ << ave << “\n”; }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Using Exception Handling double sum, count, ave; try { cin >> sum >> count; if (count <=0) throw sum; ave = sum/count; cout << …; } catch(int e) { cout << “sum is “ << e << “ but no valid count.\n”; cout << “Run the program again with valid count!\n”; }
Background image of page 4
C++ Exception Handling: try-throw-catch A try block Normal situation is handled by the code in the try block The throw statement A statement usually in the try block (may also be in the catch block – for re-throwing an exception.) The execution of a throw statement is called throwing
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 14

Exception Handling - Lecture 10 Exception Handling...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online