Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Manage Exception on Applciation based in QT and c++ languge



  • Hello everyone,
    I am developing an application in QT 5.15.2 MSVC2019 32bit in c++ language ( on Windows Machine).
    I am not very expert about this environment and, for this reason, I am getting some problems in capturing exceptions.
    Could you help me to manage the exceptions?
    Thanks ahead of time.
    Regards


  • Lifetime Qt Champion

    @Fiore said in Manage Exception on Applciation based in QT and c++ languge:

    Could you help me to manage the exceptions?

    What exceptions? Qt by itself does not use exceptions at all.



  • @Christian-Ehrlicher said in Manage Exception on Applciation based in QT and c++ languge:

    What exceptions? Qt by itself does not use exceptions at all.

    I am finding problems with it and for me is unusual. I am putting an example of the code:

    try{
    ...
    }catch (std::exception &e){
    
            bar->showMessage(tr("Exception btnSaveConfigSW: %1").arg(e.what()));
    
        }catch (...){
    
            bar->showMessage("Exception btnSaveConfigSW unknown");
        }
    

    P.S. Sometimes we donìt know which exception can come back.


  • Lifetime Qt Champion

    I am finding problems with it and for me is unusual.

    What does this mean? Does your code not work? What are you doing inside the try/catch - block?



  • @Christian-Ehrlicher said in Manage Exception on Applciation based in QT and c++ languge:

    What does this mean? Does your code not work? What are you doing inside the try/catch - block?

    I have code where I want to capture the exception if there will be. Now, any exception there is, it doesn't capture but the application collapses.



  • @Fiore said in Manage Exception on Applciation based in QT and c++ languge:

    I have code where I want to capture the exception if there will be.

    As @Christian-Ehrlicher has said, Qt does not use/raise any exceptions. So yours must be a general question about C++, nothing to do with Qt.

    Now, any exception there is, it doesn't capture but the application collapses.

    Nobody can say from what you have shown. You were asked what is inside your try block, but you didn't reply. try ... catch certainly does capture any (catchable) exceptions it sees. At a guess, you are not talking about an exception but something else non-catchable occurring. For example, if you indirect off a nullptr (a common issue in code) that is not catchable, try ... catch will not see it and your program will exit instead.



  • You have right but maybe I didn't expose well. Inside the TRY I have a lot of code and, how you can understand, almost impossible to bring it. For this reason, I showed how I am trying to capture any exception. I agree that some exceptions I can't capture but this procedure doesn't capture anything.
    For this, I search help.
    My example:

    try{
    
     }catch (std::exception &e){
    
        bar->showMessage(tr("Exception btnSaveChange: %1").arg(e.what()));
    
        }catch (...){
    
            bar->showMessage("Exception btnSaveChange unknown");
        }
    }
    

    P.S. let me know if you need some other information to help me to implement a management of exception.
    Thanks


  • Moderators

    In C++ not everything is handled via exceptions.

    With try/catch you can catch exceptions you know that can be thrown (like std::exception in your example) and what you don't know is caught by .... There are other types of faults a program can experience though, and there will be no exception thrown.

    Some of these will call std::terminate and you can install a handler for that with std::set_terminate, others will directly call std::abort or just straight crash without any handler call.

    Not everything is recoverable from. Some types of errors will terminate your application no matter what and you can't handle them gracefully by showing a message like in your example. Showing a message or even just catching an exception is a complex operation. One possible problem is an out of memory error or stack overflow. You wouldn't be able to show any message in such case anyway, since it requires allocating additional memory which you run out of. Another type of error can be writing outside of valid range. You can have stack corrupted or exception handlers overwritten. There's no way to recover from such errors and continue running the program.

    Managed languages run in a virtual machine and can recover from more serious faults, but C++ is a native language so you should not write it assuming you're just gonna catch any unknown fault, handle it and keep going. You should know what faults can occur and prepare for them specifically. If something unexpected happens - try to recover as much information about the fault as possible, e.g. gather a crash dump. Don't try to keep a faulty app running though, it's unsafe. Just let it crash and fix the bug.



  • @Fiore
    Put something like throw std::invalid_argument( "Invalid argument" ); or throw "Exception"; immediately after the try. Is that caught? So now you know that try ... catch does work.

    Then start working back on whatever code you do have to discover whatever your issue is.



  • There is not much information to go by to find out your real problem. Nevertheless, there are two things that come to mind:

    1. If your application is multithreaded you should note that GUI calls have to be managed within the GUI thread. This means that if your exception is caught in a different thread you cannot directly call showMessage(). Doing so will (or at least might) crash your app.
    2. You don't show the code inside the try block. Maybe you have wrong expectations about the use of signals/slots together with exceptions. Exceptions inside the slots cannot be safely handled outside the slot. You can try to put a try/catch around the main event loop. This will show if the exceptions take a path you don't expect. Here the catch block cannot use the GUI anymore because there is no functioning GUI left once you are outside the main event loop.

    If you are coming from a managed language you should maybe think about changing your style when writing C++. There is an ongoing discussion in the C++ community about how to handle errors properly. So far, the only thing (almost) everyone agrees on is that exceptions should only be use for exceptional behavior and not for error handling. If, e.g., a file cannot be found this is expected and not exceptional. There are only very few places in C++ where exceptions are thrown, like out-of-memory or dynamic cast on a reference. The likeliest reason why you want to catch exceptions is that you throw them yourself. Then you should maybe look for other ways to report and handle errors. Exceptions don't play nice with the asynchronous nature of signals and slots.


Log in to reply