diff --git a/src/exceptions.h b/src/exceptions.h index 21c4a2ed..9d01e14f 100644 --- a/src/exceptions.h +++ b/src/exceptions.h @@ -1,16 +1,14 @@ #include -class RecursionException: public std::exception { +class RecursionException: public std::runtime_error { public: - RecursionException(const char *recursiontype, const std::string &name) - : rectype(recursiontype), name(name) {} - virtual ~RecursionException() throw() {} - virtual const char *what() const throw() { + static RecursionException create(const char *recursiontype, const std::string &name) { std::stringstream out; - out << "ERROR: Recursion detected calling " << this->rectype << " '" << this->name << "'"; - return out.str().c_str(); - } + out << "ERROR: Recursion detected calling " << recursiontype << " '" << name << "'"; + return RecursionException(out.str()); + } + virtual ~RecursionException() throw() {} + private: - const char *rectype; - const std::string name; + RecursionException(const std::string &what_arg) : std::runtime_error(what_arg) {} }; diff --git a/src/expr.cc b/src/expr.cc index 77c5ac77..68a30891 100644 --- a/src/expr.cc +++ b/src/expr.cc @@ -473,7 +473,7 @@ ExpressionFunctionCall::ExpressionFunctionCall(const std::string &funcname, ValuePtr ExpressionFunctionCall::evaluate(const Context *context) const { if (StackCheck::inst()->check()) { - throw RecursionException("function", funcname); + throw RecursionException::create("function", funcname); } EvalContext c(context, this->call_arguments); diff --git a/src/func.cc b/src/func.cc index d4e6cbbe..e1dcc607 100644 --- a/src/func.cc +++ b/src/func.cc @@ -156,7 +156,7 @@ ValuePtr FunctionTailRecursion::evaluate(const Context *ctx, const EvalContext * tmp.setVariables(definition_arguments, &ec); c.apply_variables(tmp); - if (counter++ == 1000000) throw RecursionException("function", this->name); + if (counter++ == 1000000) throw RecursionException::create("function", this->name); } ValuePtr result = endexpr->evaluate(&c); diff --git a/src/module.cc b/src/module.cc index 715cd337..78a93e8b 100644 --- a/src/module.cc +++ b/src/module.cc @@ -156,7 +156,7 @@ AbstractNode *ModuleInstantiation::evaluate(const Context *ctx) const try { AbstractNode *node = ctx->instantiate_module(*this, &c); // Passes c as evalctx return node; - } catch (RecursionException &e) { + } catch (const RecursionException &e) { PRINT(e.what()); return NULL; } @@ -181,7 +181,7 @@ Module::~Module() AbstractNode *Module::instantiate(const Context *ctx, const ModuleInstantiation *inst, EvalContext *evalctx) const { if (StackCheck::inst()->check()) { - throw RecursionException("module", inst->name()); + throw RecursionException::create("module", inst->name()); return NULL; }