Results 1 to 7 of 7

Thread: Suggested Error Handling

  1. #1
    Join Date
    Apr 2007
    Posts
    20
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Suggested Error Handling

    Dear forum,

    I've been looking in the docs for an error handling method but haven't seen one. In the past (Pre-Qt) I've used a system of error return values defined by Microsoft, but I don't know if that is portable.

    I know Qt avoids the C++ exception handling system so is there an accepted, consistent, portable error handling approach you can suggest?
    Thanks,
    Max

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Suggested Error Handling

    Qt doesn't avoid exceptions. It just doesn't use them which doesn't mean you can't use them in your applications. In general Qt tends to return true/false values or some kind of state value from its methods, but you don't have to follow the behaviour in your classes.

  3. #3
    Join Date
    Apr 2007
    Posts
    20
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Suggested Error Handling

    Thanks for the reply.

    I like to avoid exceptions, too, for many of the same reasons as Qt does. I tend to return a status code. I was just wondering if there is a standard way of organizing status codes in Qt.

    Max

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Suggested Error Handling

    true/false and enums

  5. The following user says thank you to wysota for this useful post:

    Max Yaffe (15th June 2007)

  6. #5
    Join Date
    Oct 2011
    Location
    Australia
    Posts
    29
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Suggested Error Handling

    Quote Originally Posted by wysota View Post
    Qt doesn't avoid exceptions. It just doesn't use them which doesn't mean you can't use them in your applications. In general Qt tends to return true/false values or some kind of state value from its methods, but you don't have to follow the behaviour in your classes.
    The thing I find frustrating about trying to be like Qt and avoiding exceptions is constructors - which don't have a return value. Exceptions are the recommended C++ way of dealing with failures in constructors - AFAIK. The problem with not using exceptions in the constructor is if you don't, your initialization is not atomic. Using exceptions there is a strong contract between the client and constructor: Either you initialize successfully or you blow up. Without exceptions you have to do your init() in two phases. The issue here is clients can forget they have to do that. To guard against that you have to add a guard to all your public functions with a `am_i_initialized()` check. That's extra work, and the coder can forget to do that too.

    Long story short; with respect to exceptions, I *personally* have made a rule that I'm allowed to use exceptions in constructors only. I'm seeing how it pans out .

    But there is another piece to the puzzle; actually handling erroneous conditions consistently on an app wide basis. Exceptions are a good way to delegate error handling up the call chain towards a global handler. But the model does not really fit event drive programing anyway. The execution is not really hierarchical - you hit the event loop after a few short steps up the call chain.

    I'm still investigating, but right now, in a single threaded app, I'm wrapping the call to exec() in a ~global exception handler (that I think should catch all exceptions??) to cover exceptions off, and looking at another global mechanism for raising errors. I'm thinking I might just use qFatal() for that...

    P.S. I wanted to ask a similar question so advice/thoughts appreciated.

    Thanks.

  7. #6
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Suggested Error Handling

    Quote Originally Posted by 33333 View Post
    The thing I find frustrating about trying to be like Qt and avoiding exceptions is constructors - which don't have a return value.
    I don't do anything in the default constructor or destructor for this very reason. I use the constructor initialization lists to do basic variable initialization (NULL for pointer, 0 for int, etc) and then I add init() and cleanup() methods for each class that I implement so that I can determine whether or not initialization or cleanup has been successful.

    I personally don't use exception handling and prefer that methods return success/failure indicators... I use either true/false for simple success/failure and use enums if I need to return different success/error values.

    Hope that helps.

    Jeff

  8. #7
    Join Date
    Oct 2012
    Posts
    132
    Thanks
    10
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Suggested Error Handling

    @33333: I think this FAQ will answer some of your questions: http://www.parashift.com/c++-faq-lite/exceptions.html

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.