Note that there are some explanatory texts on larger screens.

plurals
  1. POIs that a good idea to define exception with template?
    primarykey
    data
    text
    <p>I am thinking is that a good idea to define exception with template. Defining different types of exception is a super verbose task. You have to inherit exception, there is nothing changed, just inherit. Like this..</p> <pre><code>class FooException : public BaseException { public: ... }; class BarException : public BaseException { public: ... }; ... </code></pre> <p>That's a nightmare isn't it? So I am considering to define different exception with template</p> <pre><code>/** @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 &amp;what, const string &amp;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 &amp;stream = cerr) const throw() { stream &lt;&lt; "# RunTimeError #" &lt;&lt; endl; stream &lt;&lt; "Error : " &lt;&lt; what() &lt;&lt; endl; stream &lt;&lt; "File : " &lt;&lt; getFileName() &lt;&lt; endl; stream &lt;&lt; "Line : " &lt;&lt; getLine() &lt;&lt; endl; } }; /** @brief Template exception of radio **/ template &lt;typename T&gt; class TemplateException : public Exception { public: TemplateException (const string &amp;what, const string &amp;File, size_t Line) throw() : Exception(what, File, Line) {} virtual ~TemplateException () throw() {} }; } #define THROW(type, error) (throw TemplateRadioException&lt;type&gt;( (error), __FILE__, __LINE__)) </code></pre> <p>So if I have to define a new exception, I can just define a empty class like this.</p> <pre><code>class NuclearException {}; </code></pre> <p>To throw exception</p> <pre><code>THROW(NuclearException , "Boom!!"); </code></pre> <p>To catch</p> <pre><code>try { } catch (TemplateException&lt;NuclearException&gt; &amp;e) { // ... } </code></pre> <p>If we want to catch all exception, we can write this</p> <pre><code>try { } catch (Exception &amp;e) { // ... } </code></pre> <p>It works fine, but I don't sure are there any side effects? Is this a good idea to define different type of exception? Or there is better solution? I have no idea :S</p> <p>Thanks. Victor Lin.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload