Solved Problem exceptions from static library
-
In static library I have defined:
#include <QException> namespace syntaxhl { class SynException: public QException { protected: int code; int level; QString _msg; public: explicit SynException(const char* message, int code=0, int level=0); explicit SynException(const QString& message, int code=0, int level=0); virtual const char* what() const noexcept override; int getCode() noexcept; int getLevel() noexcept; void raise() const override; SynException *clone() const override; }; }
In main module I have:
repository = nullptr; try { repository = new syntaxhl::Repository(config.pathToSyntax, config.pathToThemes); } catch (syntaxhl::SynException &e) { delete repository; QMessageBox::warning(nullptr, "Warning", e.what(), QMessageBox::Ok); repository = new syntaxhl::Repository("", ""); }
and in messageBox are trash, but if I set static library as main project and set program as my caller, it will be .. ok (!)[but only for Windows, with Linux still errors], only ok in this case, if I call program directly from WIndows are errors.
-
QException has no message... and I must subclass it
-
@AndrzejB said in Problem exceptions from static library:
and in messageBox are trash, but if I set static library as main project and set program as my caller, it will be .. ok (!)[but only for Windows, with Linux still errors], only ok in this case, if I call program directly from WIndows are errors.
Can you explain what exactly does not work? 'errors' is not really useful...
And maybe a simple test app to show the problem. -
Old good solution: instead QString, I use char buffer + new/delete + strcpy:
#ifndef EXCEPTION_H #define EXCEPTION_H #include <QException> namespace syntaxhl { class SynException: public QException { protected: int code; int level; /** Error message. */ char *msgbuf; public: /** Constructor (C++ STL strings). * @param message The error message. */ explicit SynException(const QString& message, int code=0, int level=0); ~SynException() override; /** Returns a pointer to the (constant) error description. * @return A pointer to a const char*. The underlying memory * is in posession of the Exception object. Callers must * not attempt to free the memory. */ virtual const char* what() const noexcept override; int getCode() noexcept; int getLevel() noexcept; void raise() const override; SynException *clone() const override; }; } #endif // EXCEPTION_H
#include "synexception.h" using namespace syntaxhl; SynException::SynException(const QString& message, int code, int level):code(code),level(level) { int len = message.length(); msgbuf = new char[size_t(len) + 1]; if (!msgbuf) return; strcpy(msgbuf, message.toStdString().c_str()); } SynException::~SynException() { delete msgbuf; } const char* SynException::what() const noexcept{ return msgbuf; } int SynException::getCode() noexcept { return code; } int SynException::getLevel() noexcept { return code; } void SynException::raise() const { throw *this; } SynException *SynException::clone() const { return new SynException(*this); }
-
Maybe my problem was bacause I return previously for _msg of type QString:
_msg.toStdString().c_str()
and toStdString() makes temporal std-string which was canceled before function returns?
If std-string will class field, will be Ok? -
Hi,
Out of curiosity, why are you returning a
char *
since the way you use it, the return value will be again changed to a QString ?On a side, note, you are correct, you were returning a pointer to temporary data.
-
Because standard virtual Exception method what() returns char*.
Maybe I don'y override what() but write second similar method returning QString? -
The return type doesn't enter in the method signature therefore you can't have two methods with the same parameters and different return types.
-
I can write method QString mywhat() but it would be visible only with my type. Better is using what() returning char* because I can catch all std::exception , not only QException and MyException
-
No problem with that. As written, it was just curiosity.