[Qt-interest] Qt and exceptions
John McClurkin
jwm at nei.nih.gov
Tue Apr 14 17:53:04 CEST 2009
Sorry about attaching to an old thread but my question regards Peter
Hackett's post below. I have a program that, when a corrupt data file
was read, caused exceptions to be thrown in the event loop, causing the
program to terminate as specified in the docs. In searching the mailing
list, I found this post and overrode the QApplication::notify function
as described below. Now my application runs correctly, skipping over
corrupt data. My observation is: while overriding QApplication::notify
seems necessary, none of the code in the catch blocks seems to be
executed. The qDebug() prints in the catch blocks in the rest of my code
are printed, but none of the qDebug() prints in the catch blocks in my
notify() functions are printed. Is that to be expected?
Peter Hackett wrote:
> I agree that this isn't a good place to handle it. However, if you do
> get to here and it hasn't
> been handled, it's better to handle it yourself than let Qt do it (which
> provides no info at all.)
> Here's my code:
>
> bool
> IqtApplication::notify(QObject* receiverIn,QEvent* eventIn)
> {
> try
> {
> return QApplication::notify(receiverIn,eventIn);
>
> } catch ( std::exception& e ) {
> std::cerr << "IqtApplication::notify: std exception "
> << e.what() << std::endl;
> } catch ( oaException e ) {
> std::cerr << "IqtApplication::notify: unhandled oa exception:\n"
> << e.getMsg() << std::endl;
> } catch ( ... ) {
> std::cerr << "IqtApplication::notify: unknown exception" <<
> std::endl;
> throw;
> }
> return false;
> }
>
> Anthony Serdyukov wrote:
>> Since you live in the qt world, all your code is called from the event
>> loop, and there your exception is thrown. So, you can override the
>> notify() method and surround the inherited call with try block.
>> However, i dont think it's a good idea to catch exception on this
>> level - you just cant handle it properly here. If you describe your
>> exception architecture in details, the advise could be more useful.
>>
>> 2008/12/10, Stephan Rose <kermos at somrek.net>:
>>
>>> On Wed, 2008-12-10 at 20:40 +0700, Anthony Serdyukov wrote:
>>>
>>>> You need to override notify() method of QCoreApplication, as far as i
>>>> remember.
>>>>
>>> I looked at that but that just seems to deal with events, there is no
>>> mention about exceptions in the docs. I also don't see anything in the
>>> function that would help me allow dealing with exceptions.
>>>
>>> Hmm, maybe I need to see if I can find a better way to do this somehow.
>>>
>>> Thanks,
>>>
>>> Stephan
>>>
>>>
>>> _______________________________________________
>>> Qt-interest mailing list
>>> Qt-interest at trolltech.com
>>> http://lists.trolltech.com/mailman/listinfo/qt-interest
>>>
>>>
>>
>>
>>
>
More information about the Qt-interest-old
mailing list