Skip to content

Commit

Permalink
Fix error message + add RuntimeError (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
chengcli authored Jun 29, 2023
1 parent bdd8da8 commit ca2e3cd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Application::Logger::Logger(std::string name) {
cur_monitor_ = app->GetMonitor(name);
cur_monitor_->Enter();
} else {
throw NotFoundError("Monitor " + name);
throw NotFoundError("Logger", "Monitor " + name);
}
}

Expand Down Expand Up @@ -157,7 +157,7 @@ std::string Application::FindInputFile(const std::string& name) {
if (fin) {
return full_name;
} else {
throw NotFoundError("Input file " + name);
throw NotFoundError("FindInputFile", "Input file " + name);
}
}
}
Expand All @@ -169,7 +169,7 @@ std::string Application::FindInputFile(const std::string& name) {
if (fin) {
return name;
} else {
throw NotFoundError("Input file " + name);
throw NotFoundError("FindInputFile", "Input file " + name);
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/exceptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,11 @@ std::string IndexError::GetMessage() const {
arrayName_.c_str(), m_, mmax_);
return buf;
}

std::string RuntimeError::GetMessage() const {
char buf[160];
snprintf(buf, sizeof(buf),
"RuntimeError: Expect %f but get %f for variable %s.",
expect_, val_, var_.c_str());
return buf;
}
17 changes: 17 additions & 0 deletions src/exceptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,21 @@ class InvalidValueError : public ExceptionBase {
virtual std::string GetClass() const { return "InvalidValueError"; }
};

//! An general runtime error
class RuntimeError : public ExceptionBase {
public:
//! @param func Name of the unimplemented function, such as
//! `ClassName::functionName`
RuntimeError(const std::string& func, std::string const& var,
double expect, double val)
: ExceptionBase(func), var_(var), expect_(expect), val_(val) {}

virtual std::string GetMessage() const;
virtual std::string GetClass() const { return "RuntimeError"; }
protected:
std::string var_;
double expect_;
double val_;
};

#endif // SRC_EXCEPTIONS_HPP_

0 comments on commit ca2e3cd

Please sign in to comment.