Important: Please read the Qt Code of Conduct -

Exception is not catched in QThread

  • Hi,
    I'm trying to use the exiv2 library to modify image metadata. This library uses C++ exceptions. My code is working in the main thread ( e.g. in the constructor of the QThread ), but when I move it to the threads run() method I the exception does not get caught and the program crashes:

    try {
    } catch ( ... ) {
        qDebug("got it!");

    I'm using the MSVC2015 and Qt 5.9.1

  • Hi! Just tested it on desktop Linux. Created a widgets projects, added a pushbutton to the mainwindow. And created a worker class for the thread:

    #ifndef WORKER_H
    #define WORKER_H
    #include <QObject>
    class Worker : public QObject
        Worker(QObject *parent = nullptr);
    public slots:
        void process();
        void finished();
    #endif // WORKER_H


    #include "worker.h"
    #include <unistd.h>
    #include <exiv2/exiv2.hpp>
    #include <QDebug>
    Worker::Worker(QObject *parent)
        : QObject(parent)
    void Worker::process()
        try {
        } catch (...) {
            qDebug() << "meow";
        emit finished();

    in mainwindow.cpp

    void MainWindow::on_pushButton_clicked()
        ui->pushButton->setEnabled(false); // ignore this
        auto t = new QThread;
        auto w = new Worker;
        connect(t, &QThread::started, w, &Worker::process);
        connect(w, &Worker::finished, t, &QThread::quit);
        connect(w, &Worker::finished, w, &Worker::deleteLater);
        connect(t, &QThread::finished, t, &QThread::deleteLater);
        connect(t, &QThread::finished, this, &MainWindow::enableButton);

    So far, works as expected. Do you do anything else with Exiv besides that in your code / in another thread? Maybe some part of the exiv library isn't thread-safe.

  • Hi Wieland,

    I've just tested your code on Windows/MSVC2015 and it crashed. I've also tested my code on Linux and there it works.

    From the beginning I would have been glad to develop under Linux, but I rely on some Windows hardware driver.

    One Question regarding your code: You included unistd.h . Is this for purpose or some leftover?

    Have you some more idea what could be the reason?

Log in to reply