1

I made a subclass of QException as follows:

#include <QtConcurrent>
#include <QException>

class ProcessingException : public QException
{
public:
    ProcessingException(QString const& message) :
        message(message)
    {}

    virtual ~ProcessingException()
    {

    }

    void raise() const { throw *this; }
    ProcessingException *clone() const { return new ProcessingException(*this); }

    QString getMessage() const
    {
        return message;
    }
private:
    QString message;
};

From a method that's run by QtConcurrent::run I throw an instance of this subclass with throw new ProcessingException("test");. Then if I understand the documentation properly, when I collect the future's result with .waitForFinished(), Qt should re-throw the QException subclass. However, using this approach I'm only able to catch a QUnhandledException:

try
{
    watcher->waitForFinished();
}
catch(const ProcessingException &e)
{
    qCritical() << "Caught:" << e.getMessage();
}
catch(const QUnhandledException &e)
{
    qCritical() << "Uncaught qexception!";
}

What am I missing?

Mathijs
  • 347
  • 1
  • 4
  • 13

1 Answers1

4

You are throwing a pointer but catching a reference....

Just use throw ProcessingException("test");. You can catch it by const reference and you don't have to clean up (call delete) after catching it.

Related: throw new std::exception vs throw std::exception

Community
  • 1
  • 1
Simon Kraemer
  • 5,700
  • 1
  • 19
  • 49