Is it a good idea to define an exception with a pattern?

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() {} }; } #define THROW(type, error) (throw TemplateRadioException<type>( (error), __FILE__, __LINE__)) 

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.

+4
source share
3 answers

It is definitely possible and works great, but I would avoid it. He hides the diagnosis. GCC will display the name of the exception type, including regular template material. I would take a few minutes to define a new exception class personally. It is not as if you were doing this all the time.

+2
source

This is an interesting idea, but in addition to the noted drawbacks, it also does not allow you to define a hierarchy of exceptions: suppose you want to define

 class InvalidArgumentException {}; class NullPointerException : public InvalidArgumentException {}; 

then TemplatedException <NullPointerException> does not inherit from TemplatedException <InvalidArgumentException>, and your exception handling mechanism may be more awkward than "simple".

+3
source

If you're lazy and don't want to write what you need to declare a new exception class, you can always use a macro that does this for you.

+2
source

All Articles