Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8969775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:38:27+00:00 2026-06-15T17:38:27+00:00

I know it doesn’t make sense to actually handle an exception thrown in a

  • 0

I know it doesn’t make sense to actually handle an exception thrown in a different thread, but is there some way I can get notified that at least an exception occurred? E.g. something like

#include <QtConcurrentRun>

#include <iostream>
#include <stdexcept>

void MyFunction()
{
//  std::cout << "MyFunction()" << std::endl;
  throw std::runtime_error("Test exception.");
}

int main()
{
  try
  {
    QtConcurrent::run(MyFunction);
  }
  catch(...)
  {
    std::cout << "Exception caught!" << std::endl;
  }

}

exits quietly, even though an exception occurred. This is sometimes very confusing when the exception comes from deep down in the call stack somewhere.

————EDIT————-

I tried to write a wrapper like UmNyobe suggested, but I must be doing something wrong with the function pointers?

#include <QtConcurrentRun>
#include <QFutureWatcher>
#include <QObject>

#include <iostream>
#include <stdexcept>

void MyFunction()
{
//  std::cout << "MyFunction()" << std::endl;
  throw std::runtime_error("Test exception.");
}

template<typename TFirstParam, typename... TParams>
bool ExceptionWrapper(TFirstParam firstParam, TParams&& ...params)
{
  // Here 'firstParam' should be a function pointer, and 'params' are the arguments
  // that should be passed to the function
  try
  {
    firstParam(params...);
  }
  catch(...)
  {
    std::cout << "Exception caught!" << std::endl;
    return false; // failure
  }

  return true; // success
}

struct MyClass : public QObject
{
  Q_OBJECT

  MyClass()
  {
    connect(&this->FutureWatcher, SIGNAL(finished()), this, SLOT(slot_finished()));
  }

  void DoSomething()
  {
    void (*myFunctionPointer)() = MyFunction;
    bool (*functionPointer)(decltype(myFunctionPointer)) = ExceptionWrapper;

    QFuture<bool> future = QtConcurrent::run(functionPointer);
    this->FutureWatcher.setFuture(future);
  }

  QFutureWatcher<void> FutureWatcher;

  void slot_finished()
  {
    std::cout << "Finished" << std::endl;
    if(!this->FutureWatcher.result())
    {
      std::cout << "There was an error!" << std::endl;
    }
  }
};

#include "ExceptionWrapper.moc"

int main()
{
  MyClass myClass = new MyClass;
  myClass->DoSomething();
}

The error I get is on this line:

QFuture<bool> future = QtConcurrent::run(functionPointer);

error: no matching function for call to 'run(bool (*&)(void (*)()))'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:38:27+00:00Added an answer on June 15, 2026 at 5:38 pm

    I know it doesn’t make sense to actually handle an exception thrown in a different thread, but is there some way I can get notified that at least an exception occurred?

    You can handle it by using the future returned from QtConcurrent::run. See this page for details. When you collect on the future, any unhandled exceptions will be rethrown. You can make a simple wrapper class to capture an exception and examine it in the receiving thread.

    #include <QtGui>
    #include <iostream>
    #include <stdexcept>
    
    class MyException : public QtConcurrent::Exception
    {
    public:
        MyException(std::exception& err) : e(err) {}
        void raise() const { throw *this; }
        Exception* clone() const { return new MyException(*this); }
        std::exception error() const { return e; }
    private:
        std::exception e;
    };
    
    // first concurrent function
    int addFive(int n)
    {
        try
        {
            throw std::runtime_error("kablammo!");
            //throw -1;
            return n + 5;
        }
        catch (std::exception& e)
        {
            throw MyException(e);
        }
    
    }
    
    // second concurrent function    
    void myVoidFunction()
    {
        try
        {
            throw std::runtime_error("oops!");
            //throw -1;
        }
        catch (std::exception& e)
        {
            throw MyException(e);
        }
    }
    
    int main(int argc, char* argv[])
    {
        QApplication app(argc, argv);
    
        QFuture<int> f1 = QtConcurrent::run(addFive, 50);
        try
        {
            int r = f1.result();
            std::cout << "result = " << r << std::endl;
        }
        catch (MyException& me)
        {
            std::cout << me.error().what() << std::endl;
        }
        catch (QtConcurrent::UnhandledException&)
        {
            std::cout << "unhandled exception in addFive\n";
        }
    
        QFuture<void> f2 = QtConcurrent::run(myVoidFunction);
        try
        {
            // result() not available for QFuture<void>, use waitForFinished() to
            // block until it's done.
            f2.waitForFinished();
            std::cout << "myVoidFunction finished\n";
        }
        catch (MyException& me)
        {
            std::cout << me.error().what() << std::endl;
        }
        catch (QtConcurrent::UnhandledException&)
        {
            std::cout << "unhandled exception in myVoidFunction\n";
        }
    
        QWidget w;
        w.show();
    
        return app.exec();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know it doesn't necessarily make any sense, but is there a way to
I know Python doesn't have pointers, but is there a way to have this
I know this doesn't work but is there any way of autogenerating values? It
I know it doesn't make a lot of sense, but I have a map
I know CodeDom doesn't support partial methods, but is there a workaround? I found
I know it doesn't exist, but is there a pure CSS version? Would like
I know Android doesn't support MJPEG natively but are there any jar files/drivers available
I know this doesn't sound productive, but I'm looking for a way to remember
So I know as3 doesn't have pointers, but I thought there might be a
I know it doesn't actually act on it, but when I test IE 9

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.