How to catch all exceptions, even without throw in Qt project?



  • Hello,

    I'm trying to build a test project in Qt Creator, where I want to catch all exceptions in single catch block even without throw.

    For example, the following code works in MS Visual Studio without crash, if I set the option C/C++ -> Code Generation -> Enable C++ Exceptions to /EHa:

    int a = 100, b = 0, c = 1;
    try
    {
        c = a / b;
    }
    catch (...)
    {
        c = 0;
    }
    

    But the same code above crashes in Qt Creator (as expected). Is there any QMake flag or option (similar to /EHa option in MS Visual Studio) I can set in Qt .pro file, so that I can avoid crash and catch all exceptions without throw?

    Thanks in advance.


  • Qt Champions 2016

    @soma_yarram
    Hi
    Maybe something like this
    Menu: Tools->Options->Debugger
    alt text


  • Moderators

    @soma_yarram said in How to catch all exceptions, even without throw in Qt project?:

    Is there any QMake flag or option (similar to /EHa option in MS Visual Studio) I can set in Qt .pro file, so that I can avoid crash and catch all exceptions without throw?

    /EHa is a compiler flag. You can pass compiler flags via qmake in the .pro file like this:

    QMAKE_CXX_FLAGS += /EHa
    


  • @mrjj Thanks for the reply, but I don't see any CDB tab in my Debugger option. Moreover, I don't think it is related to debugger, I want to have a compiler option which can treat system level errors (e.g. divide-by-zero) as a normal exception.


  • Qt Champions 2016

    @soma_yarram
    Hi
    I thought of debugging. sorry.

    Well Chris shows how to add the flag

    but I its unclear which compiler , you are using ?



  • @Chris-Kawa Thanks for the reply, but it doesn't work. App still crashes while dividing by zero.



  • @mrjj I use GCC compiler under linux and mac platforms.


  • Qt Champions 2016

    You assume division by zero is an exception. As it happens it's an error that the ALU encounters when trying to execute the corresponding asm instruction and how that's handled depends on the OS, but usually (i.e. with the exception of Windows) it is not an exception. What one usually does to protect the code is to add assertions, so such errors can be caught at debug time, e.g.:

    Q_ASSERT(b);
    c = a / b;
    


  • @kshegunov I agree to your statement. I know it is safe to check the values before doing anything, but I just want to know any flag/option (similar to /EHa in MS Visual Studio) I can set in Qt .pro file, which is loaded by IDE Qt Creator (using GCC compiler) under linux and mac platforms, which can avoid crash, treat system error as a normal exception, and execute catch block immediately. Any help would be much appreciated. Thanks.


  • Qt Champions 2016

    @soma_yarram said in How to catch all exceptions, even without throw in Qt project?:

    but I just want to know any flag/option (similar to /EHa in MS Visual Studio) I can set in Qt .pro file, which is loaded by IDE Qt Creator (using GCC compiler) under linux and mac platforms

    The point is, there's no such flag! What you should do is check your value before dividing by it. And by the way dividing something by zero is undefined, so your example code is badly posed even in the case there's an exception thrown.



  • @kshegunov Thanks for the comment. Divide-by-zero is just an example I gave. One more example can be accessing invalid pointers as below:

    QWidget *pWidget;
    
    try
    {
        if (pWidget)
        {
            pWidget->setObjectName("Hello");
        }
    }
    catch (...)
    {
        std::cout << "something went wrong";
    }
    

    As the pWidget points to invalid location, and the check if (pWidget) succeeds, the code above definitely crashes. I know that initializing pointer pWidget to NULL would solve the issue, but the crash can be avoided with MCVC compiler flag /EHa, and I only want to know the corresponding flag for GCC compiler.


  • Qt Champions 2016

    @soma_yarram said in How to catch all exceptions, even without throw in Qt project?:

    I only want to know the corresponding flag for GCC compiler.

    There's none. Segmentation fault (what dereferencing dangling pointers cause) can be caught by trapping the corresponding POSIX signal, but that's a last resort measure and usually in the handler one puts ::exit() to terminate the application. This signal is not raised for catching bad code, but as a means to do final cleanup (like closing driver handles and the such) before the runtime terminates the application.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.