Skip to content

Instantly share code, notes, and snippets.

@cjhanks
Created March 13, 2014 20:23
Show Gist options
  • Save cjhanks/9536236 to your computer and use it in GitHub Desktop.
Save cjhanks/9536236 to your computer and use it in GitHub Desktop.
safe-except_dog
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
class T {
public:
T(const string& value)
: value(value)
{
if (value == "fish")
throw std::runtime_error("No fish allowed");
}
int
call(int k) {
if (value == "dog")
return 4;
else
return 3 * k;
}
private:
const string value;
};
int main () {
{
T* ptr = nullptr;
try {
ptr = new T("fish");
} catch (...) {}
if (ptr)
ptr->call(4);
}
{
T* ptr = nullptr;
try {
ptr = new T("dog");
} catch (...) {}
if (ptr)
ptr->call(4);
}
{
T* ptr = nullptr;
try {
ptr = new T("cow");
} catch (...) {}
if (ptr)
ptr->call(4);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment