Created
May 8, 2012 17:01
-
-
Save jzrake/2637327 to your computer and use it in GitHub Desktop.
Quick tutorial on runtime exceptions in C++
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
#include <iostream> | |
#include <stdexcept> | |
class ZrakeFailed : public std::runtime_error | |
{ | |
public: | |
ZrakeFailed(const std::string &msg) : std::runtime_error("zraker failed. " + msg) { } | |
} ; | |
class BhaskarFailed : public std::runtime_error | |
{ | |
public: | |
BhaskarFailed(const std::string &msg) : std::runtime_error("bhaskar failed. " + msg) { } | |
} ; | |
int main() | |
{ | |
try { | |
throw ZrakeFailed("yoga anyone?"); | |
throw BhaskarFailed("beer anyone?"); | |
} | |
catch (const ZrakeFailed &e) { | |
std::cout << "ugh. knee?" << std::endl; | |
std::cout << e.what() << std::endl; | |
} | |
catch (const std::exception &e) { | |
std::cout << e.what() << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment