PDA

View Full Version : QThread Error Handling



TheJim01
7th April 2010, 17:02
Hi all,

I'm starting to use QThreads to break some I/O and computationally intensive functions away from my GUI. I'm at the level of understanding that I know how to create/start/poll/destroy threads, and have inter-thread communication working. I'm now looking at error handling, but can't find any information--perhaps I'm wording my queries wrong?

I have a function which would throw an exception upon encountering an error. This function is one I wish to move to its own thread. From what I can see, I can use inter-thread messaging to keep track of the thread's process, and if it quits before reaching "100%", then an error occurred, and I can derive the type from the percentage (the first 10% will be file read errors, etc.).

But I don't want to implement anything before seeing if there is an "official" implementation of error handling for threads. If I can override QThread::run() to throw an exception, that's great, I wouldn't have to re-write some of my code. But If I have to use the above method, so be it. If anyone has any links to docs or examples, they'd be much appreciated. Thanks! :D

wysota
7th April 2010, 21:50
I would recommend against QThread::run() throwing exceptions. You can throw an exception in some piece of code called from run() and catch it in run() but it is vital the exception doesn't "leak out" of run(). What I can recommend is that you simply emit a signal from your processing object and let that signal carry information about the error. There is really no need for exceptions here.

TheJim01
8th April 2010, 14:54
I use signals for reporting progress, so I don't know why I overlooked using them to report exceptions. Thank you very much!