Skip to content

Instantly share code, notes, and snippets.

@jzrake
Created May 8, 2012 17:01
Show Gist options
  • Save jzrake/2637327 to your computer and use it in GitHub Desktop.
Save jzrake/2637327 to your computer and use it in GitHub Desktop.
Quick tutorial on runtime exceptions in C++
#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