Skip to content

Instantly share code, notes, and snippets.

@lshort
Created June 30, 2014 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lshort/752fb40964adfb2e1601 to your computer and use it in GitHub Desktop.
Save lshort/752fb40964adfb2e1601 to your computer and use it in GitHub Desktop.
Improved expect_exception
template<typename ExecLambda, typename OnThrowLambda, typename NoThrowLambda>
auto expect_exception( ExecLambda exec_lambda, bool expect_to_throw,
OnThrowLambda exc_lambda, NoThrowLambda no_exc_lambda)
{
bool threw;
decltype( exec_lambda() ) x;
try {
x = exec_lambda();
threw = false;
}
catch (...) {
threw = true;
}
if ( expect_to_throw != threw ) {
if (expect_to_throw)
throw "failed to catch expected exception";
else
throw "caught unexpect exception";
} else {
if (expect_to_throw)
return exc_lambda();
else
return no_exc_lambda(x);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment