Exception Handling - throw without type not allowed



  • I've been caught out by this a couple of times now. I wanted to throw an exception if a function call failed but as I wasn't bothered about the reason for failure, more the robustness of the code, so I used the following :-

    try
    {
        if (!foo()) throw;
    }
    catch(...)
    {
    
    }
    

    Problem is, throw without an object doesn't seem to get caught and causes the process to crash. My workaround was to use

    #define THROW throw 1
    

    and then call THROW whenever I needed to.

    Can anyone explain why this fails as I thought this use of throw was legitimate ?

    Alan

    [Moved to C++ Gurus, added code tags ~kshegunov]


  • Qt Champions 2016

    Hi
    Im not sure you are using it correctly.
    The standard says:
    "A throw-expression with no operand rethrows the exception being handled. The exception is reactivated with the existing temporary; no new temporary exception object is created. The exception is no longer considered to be caught; therefore, the value of uncaught_exception() will again be true."

    So you are re-throwing a not existing exception and it seems not to be catchable due to missing operand.



  • @Alan-B

    I read: throw without argument is a re-throw of an exception you caught. So it is allowed only inside a catch clause.

    -Michael.



  • Many thanks for the replies. That clears it up.


Log in to reply
 

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