Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Exception Handling - throw without type not allowed
Forum Updated to NodeBB v4.3 + New Features

Exception Handling - throw without type not allowed

Scheduled Pinned Locked Moved Solved C++ Gurus
4 Posts 3 Posters 1.5k Views 1 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.
  • A Offline
    A Offline
    Alan B
    wrote on last edited by kshegunov
    #1

    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]

    m.sueM 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      4
      • A Alan B

        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]

        m.sueM Offline
        m.sueM Offline
        m.sue
        wrote on last edited by m.sue
        #3

        @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.

        1 Reply Last reply
        4
        • A Offline
          A Offline
          Alan B
          wrote on last edited by
          #4

          Many thanks for the replies. That clears it up.

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved