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. Qt5 how to catch an exception?
Qt 6.11 is out! See what's new in the release blog

Qt5 how to catch an exception?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 5 Posters 700 Views 3 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.
  • echo_lovelyE Offline
    echo_lovelyE Offline
    echo_lovely
    wrote on last edited by
    #1

    Environment


    • OS: Ubuntu 20.04LTS
    • Qt-Version: 5.12.8

    Problem


    How to catch an exception?
    if my code likes this

    try{
    	//if here occur an null_pointer_exception or io_exception,how to catch the exception?
    }catch(???){
    	???
    }
    

    or how do I turn on the switch of Qt5 and catch the exception?

    KroMignonK S 2 Replies Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

      your logic is correct but TTBOMK Qt doesn't do exceptions, so any exceptions thrown will be core c++ exceptions, and I'm not sure how well the MOC supports throwing.

      Also, your description of what an exception is is lacking. nullptr should always be manually checked, and there is no such thing as io_exception. look at the core c++ exceptions to see what stl has implemented.

      The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        ChrisW67
        wrote on last edited by
        #3

        Dereferencing a nullptr will generate an operating system exception (SIGSEGV), not a C++ exception. On Linux this leads to immediate operating system termination of your application; there's little you can do about it. You might be able to catch that signal (http://www.cplusplus.com/reference/csignal/signal/) but, unless you are absolutely certain that you can recover, your program should be considered mortally wounded. It is far easier to program defensively as @Kent-Dorfman says.

        1 Reply Last reply
        2
        • echo_lovelyE echo_lovely

          Environment


          • OS: Ubuntu 20.04LTS
          • Qt-Version: 5.12.8

          Problem


          How to catch an exception?
          if my code likes this

          try{
          	//if here occur an null_pointer_exception or io_exception,how to catch the exception?
          }catch(???){
          	???
          }
          

          or how do I turn on the switch of Qt5 and catch the exception?

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by KroMignon
          #4

          @echo_lovely said in Qt5 how to catch an exception?:

          or how do I turn on the switch of Qt5 and catch the exception?

          As written before, Qt does NOT use exception, for historical reason, and prefer use return code values to handle errors.
          There is an effort done for "exception safety", take a look at documentation for more details: https://doc.qt.io/qt-5/exceptionsafety.html

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          1 Reply Last reply
          0
          • echo_lovelyE echo_lovely

            Environment


            • OS: Ubuntu 20.04LTS
            • Qt-Version: 5.12.8

            Problem


            How to catch an exception?
            if my code likes this

            try{
            	//if here occur an null_pointer_exception or io_exception,how to catch the exception?
            }catch(???){
            	???
            }
            

            or how do I turn on the switch of Qt5 and catch the exception?

            S Offline
            S Offline
            SimonSchroeder
            wrote on last edited by
            #5

            Qt does not forbid to use exceptions in general. You should avoid to use them across signal/slot boundaries unless you are certain about what you are doing. With a direct connection exceptions across signal/slot boundaries would work as these are regular function calls internally. If signals are queued inside the event loop chaos will happen. You need to wrap the exec() into a try/catch. There is also no safe way to recover the event loop.

            You don't always have a choice if exceptions are used, especially with 3rd party libraries. Here, you have to make sure to catch all exceptions before emitting a signal. Then you are safe. This is also just plain old C++.

            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