Correct way to create custom exception.
-
hi, I'm writing a MapRendering class to render an image, and I'd like to add a custom exception: RenderingException.
Searching on the internet I write to this code:class MapRendering { public: MapRendering(QString mapsource); void rendering(qreal lat, qreal lon, const QVector<uint> range_vector); private: QString map_source; }; class RenderingException : public QException { public: RenderingException(QString const& text=" ") throw() :message(text) {} void raise() const override { throw *this; } RenderingException *clone() const override { return new RenderingException(*this); } const char* what() const throw() { return this->message.toStdString().c_str(); } private: QString message; };
No problem with the class, but there are many warnings with the exception subclass of QException:
someone can help me on how to write the subclass of QException correctly and without warning. Thanks in advance. -
hi, I'm writing a MapRendering class to render an image, and I'd like to add a custom exception: RenderingException.
Searching on the internet I write to this code:class MapRendering { public: MapRendering(QString mapsource); void rendering(qreal lat, qreal lon, const QVector<uint> range_vector); private: QString map_source; }; class RenderingException : public QException { public: RenderingException(QString const& text=" ") throw() :message(text) {} void raise() const override { throw *this; } RenderingException *clone() const override { return new RenderingException(*this); } const char* what() const throw() { return this->message.toStdString().c_str(); } private: QString message; };
No problem with the class, but there are many warnings with the exception subclass of QException:
someone can help me on how to write the subclass of QException correctly and without warning. Thanks in advance.Hi :)
- Warning: I guess you have to add a destructor.
- Warning: Deprecated -> Compiler shows suggestion to fix it.
- Warning: Add
override
afterwhat()
-
Hi :)
- Warning: I guess you have to add a destructor.
- Warning: Deprecated -> Compiler shows suggestion to fix it.
- Warning: Add
override
afterwhat()
@Pl45m4
if I add the destructor the warning on class declaration disappear, but I have this new one:maprendering.h:38:5: warning: definition of implicit copy constructor for 'RenderingException' is deprecated because it has a user-declared destructor maprendering.h:40:41: note: in implicit copy constructor for 'RenderingException' first required here
Adding override after what() I have this error:
maprendering.h:42:24: error: non-virtual member function marked 'override' hides virtual member function exception.h:69:5: note: hidden overloaded virtual function 'std::exception::what' declared here: different qualifiers (const vs none)
-
@Pl45m4
if I add the destructor the warning on class declaration disappear, but I have this new one:maprendering.h:38:5: warning: definition of implicit copy constructor for 'RenderingException' is deprecated because it has a user-declared destructor maprendering.h:40:41: note: in implicit copy constructor for 'RenderingException' first required here
Adding override after what() I have this error:
maprendering.h:42:24: error: non-virtual member function marked 'override' hides virtual member function exception.h:69:5: note: hidden overloaded virtual function 'std::exception::what' declared here: different qualifiers (const vs none)
@federico.massimi said in Correct way to create custom exception.:
exception.h:69:5: note: hidden overloaded virtual function 'std::exception::what' declared here: different qualifiers
You forgot const after method name.
"warning: definition of implicit copy constructor for 'RenderingException' is deprecated because it has a user-declared destructor" - add a copy constructor.
-
@federico.massimi said in Correct way to create custom exception.:
exception.h:69:5: note: hidden overloaded virtual function 'std::exception::what' declared here: different qualifiers
You forgot const after method name.
"warning: definition of implicit copy constructor for 'RenderingException' is deprecated because it has a user-declared destructor" - add a copy constructor.
@jsulm said in Correct way to create custom exception.:
const
Every time i remove a warning a new one appear, now my code is:
class RenderingException : public QException { public: RenderingException(QString const& text=" ") noexcept :message(text) {} RenderingException(const RenderingException &re) {this->message = re.message; } ~RenderingException() override {} void raise() const override { throw *this; } RenderingException *clone() const override { return new RenderingException(*this); } const char *what() const noexcept override { return this->message.toStdString().c_str(); } private: QString message; };
and I have just one warning on the first line (class RenderingException : public QException):
maprendering.h:33:7: warning: 'RenderingException' has no out-of-line virtual method definitions; its vtable will be emitted in every translation unit
-
You could move any (or all) of your function definitions (or the constructor) into a seperate .c or .cpp file to make the warning disappear. The warning is emitted because your class (unrelated to whether its an exception or not) doesnt have it's own compilation unit (all definitions and declarations are in the header), so it's vtable is copied in every unit that uses your class.
-
OK, Work!