C++ Exception Handling



  • Hi
    I've installed in my Desktop PC (Windows 7 Pro) the last Qt Creator 2.6.2 having already installed the MS Visual C++ 2010 Express
    I face an issue with C++ Exception handling. Specifically, when I throw an exception my application crashes. Here is the output of the Qt creator:
    [quote]The program has unexpectedly finished.
    C:\Users\George\Documents\Tirage\Tirage-build-Desktop_Qt_5_0_1_MSVC2010_32bit-Debug\debug\Tirage.exe exited with code -1073741819

    Starting C:\Users\George\Documents\Tirage\Tirage-build-Desktop_Qt_5_0_1_MSVC2010_32bit-Debug\debug\Tirage.exe...
    The program has unexpectedly finished.
    C:\Users\George\Documents\Tirage\Tirage-build-Desktop_Qt_5_0_1_MSVC2010_32bit-Debug\debug\Tirage.exe exited with code -1073741819[/quote]

    Any help will greatly appreciated because I am totally new to the Qt development environment.
    Thanks in advance.



  • Qt itself doesn't use exceptions, but it supports it. In my experience you really should stay away from exceptions in C++ programming, whatever the libraries you use.

    I'm not sure why your app would crash, other than the obvious that the exception was not caught by you. ;) Qt doesn't have a problem with them, specifically.

    But, have you considered to not throw exceptions? As I said, a huge library Qt manages just fine without them. Likely its possible to avoid them in your app too ;)



  • As you are new i have to say that these exceptions are worst things in c++ and as Zander said you have to stay away from them . just step into your code to see where it happens. You can use qDebug() to see where it happens if your debugger is not set



  • -1073741819 = 0xC0000005

    In Windows means "Access violation".

    I agree with Thomas and Mostafa (avoid exceptions). Your code probably has an error not related to Qt.



  • I think exceptions can be useful in some cases. I don't use them for everything but there are some situations they are quite suited for. Your code can be cleaner as it will focus on the task and not error handling.

    For example, if loading a file and a problem occurs the exception can describe the problem and, unlike a return value, cannot be ignored. You can use the information from the exception to pass onto the user such as missing file, truncated file, permissions, etc (instead of 'cannot load file').

    If your exceptions are caught in main() then this is an example of misuse.



  • [quote author="Thomas Zander" date="1361911475"]Qt itself doesn't use exceptions, but it supports it. In my experience you really should stay away from exceptions in C++ programming, whatever the libraries you use.[/quote]

    I am pretty sure that Qt uses exceptions internally, however it doesn't throw exceptions at the user. You are still free to use exceptions where it is adequate to, most of the times it is possible to avoid using exceptions but there are a few cases where you cannot anticipate a scenario you cannot provision against and you do need to use exceptions.



  • bq. Rondog wrote:
    I think exceptions can be useful in some cases.

    The feature as is makes sense and is used in Java extensively, as well as other more modern languages. But if you care about code quality, then avoid them in C++ as they introduce too many bad effects to in the whole be a positive thing. With C++11 (at least on clang) one huge hole is kind-of plugged, it now just crashes your app when you throw an exception in a destructor. Whereas before it leaked memory. But think about it, crahing your app when throwing an exception there is deemed an improvement over the previous design. Consider how bad people think it was before :)

    bq. utcenter wrote:
    I am pretty sure that Qt uses exceptions internally, however it doesn’t throw exceptions at the user.

    It doesn't create them, specifically. Qt only catches system exceptions (like malloc throwing an bad_alloc) since it would be wrong to not to.


  • Lifetime Qt Champion

    Hi,

    If you don't use malloc'd memory, check that all your pointers are initialized (and don't forget to set them to NULL in the constructor if you don't allocate the objects right away)



  • First of all I want to say thank you for all of your suggestions. I realised this forum is a very friendly forum.
    I have to say that disagree and I believe that using exceptions handlers, a programmer can create a central piece of code and this is tied to object oriented logic.
    I am try to learn the Qt environment and I can publish my code because it is a small project so far and you will have the oportunity to verify that there is no bug in it.
    I am not convinced yet that Qt supports exceptions.



  • bq. I am not convinced yet that Qt supports exceptions.

    Read the docs to see what Qt official position is :)
    http://doc-snapshot.qt-project.org/4.8/exceptionsafety.html


Log in to reply
 

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