I think it's a good idea to define an exception with a pattern. Defining various types of exceptions is a difficult task. You must inherit the exception, nothing has changed, just inherit. Like this..
class FooException : public BaseException { public: ... }; class BarException : public BaseException { public: ... }; ...
This is a nightmare, right? Therefore, I am considering defining another exception with a pattern
/** @brief Exception of radio **/ class Exception : public runtime_error { private: /// Name of file that throw const string m_FileName; /// Line number of file that throw size_t m_Line; public: Exception(const string &what, const string &File, size_t Line) throw() : runtime_error(what), m_FileName(File), m_Line(Line) {} virtual ~Exception() throw() {} /** @brief Get name of file that throw @return Name of file that throw **/ virtual const string getFileName() const throw() { return m_FileName; } /** @brief Get throw exception line @return Throw exception line **/ virtual size_t getLine() const throw() { return m_Line; } /** @brief Get description of this exception @return Description of this exception **/ virtual const string getMessage() const throw() { return what(); } virtual void print(ostream &stream = cerr) const throw() { stream << "# RunTimeError #" << endl; stream << "Error : " << what() << endl; stream << "File : " << getFileName() << endl; stream << "Line : " << getLine() << endl; } }; /** @brief Template exception of radio **/ template <typename T> class TemplateException : public Exception { public: TemplateException (const string &what, const string &File, size_t Line) throw() : Exception(what, File, Line) {} virtual ~TemplateException () throw() {} }; }
So, if I need to define a new exception, I can just define an empty class like this.
class NuclearException {};
Throw an exception
THROW(NuclearException , "Boom!!");
To catch
try { } catch (TemplateException<NuclearException> &e) {
If we want to catch all the exceptions, we can write this
try { } catch (Exception &e) {
It works great, but I'm not sure if there are any side effects? Is it a good idea to define a different type of exception? Or is there a better solution? I have no idea: S
Thanks. Victor Lin.