Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Exceptions alternative
Qt 6.11 is out! See what's new in the release blog

Exceptions alternative

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Exotic_Devel
    wrote on last edited by
    #1

    From what I've been reading, Qt does not use exceptions. So what would be the way to capture errors from some Qt classes? For example QFile.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Operation that might fail in Qt are generally in the form of bool methodThatMightFail() so you would check whether the method is successful and if not check the error/errorString method. Some more advanced classes are using a dedicated error class like QSqlError.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        In most cases Qt uses error codes or other meaningful return values like null pointers. For example if file can't be opened for whatever reason QFile::open() will return false.

        Often you can also query for the result of some operation. An example of that is "QNetworkReply::error()":http://qt-project.org/doc/qt-5/qnetworkreply.html#error

        If an operation is in some sense detached from the point of invocation (eg. asynchronous) Qt also emits signals. An example of that is "QNetworkAccessManager::sslErrors":http://qt-project.org/doc/qt-5/qnetworkaccessmanager.html#sslErrors

        In rare cases you can also pass an optional variable that will be filled with the result of an operation. An example of that is "QString::toInt()":http://qt-project.org/doc/qt-5/qstring.html#toInt

        If unsure how specific class handles an error refer to the docs. They are usually clear about it.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved