Created
December 1, 2012 17:37
-
-
Save ElemarJR/4183382 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <stdexcept> | |
| using namespace std; | |
| int safeDivide(int a, int b) { | |
| if (b == 0) | |
| throw invalid_argument("Division by 0"); | |
| return a / b; | |
| } | |
| int main() { | |
| try { | |
| cout | |
| << "10 / 2 = " | |
| << safeDivide(10, 2) | |
| << endl; | |
| cout | |
| << "5 / 0 = " | |
| << safeDivide(5, 0) // throws exceptions | |
| << endl; | |
| cout | |
| << "(never executed) 6 / 3 = " | |
| << safeDivide(6, 3) | |
| << endl; | |
| } catch (const invalid_argument& e) | |
| { | |
| cout << "caught exception: " << e.what() << endl; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
std::domain_erroris probably more appropriate.