Created
February 21, 2018 04:42
-
-
Save danthemango/65bce895c54f96bf6b7c0cf8cd26a8f3 to your computer and use it in GitHub Desktop.
Minimal C++ Exceptions with exception class
This file contains 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
// example program which throws a class exception | |
#include <iostream> | |
using namespace std; | |
// my error class | |
class WrongFormat{ | |
public: | |
string what(){ | |
return string("Wrong Format!"); | |
} | |
}; | |
int main(){ | |
try{ | |
// oh no, something bad happened | |
throw WrongFormat(); | |
}catch(WrongFormat w){ | |
cout << w.what() << endl; | |
} | |
} |
This file contains 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
// example program which just throws a string exception | |
#include <iostream> | |
using namespace std; | |
int main(){ | |
try{ | |
// oh no, something bad happened | |
throw string("something happen"); | |
}catch(string s){ | |
cout << "Error: " << s << endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment