https://gcc.gnu.org/bugzilla/show_bug.cgi?id=62046

            Bug ID: 62046
           Summary: A catch in a class, without any try, compiles fine
           Product: gcc
           Version: 4.8.1
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gcc at themudrest dot com

A catch can follow a method in a class, without any try. Isn't this a syntax
error?  We couldn't find anything in the standard explaining it.

This is a vector for bugs in code, if your forget to include the try after a
method, the expected catch for throws in the method is not called.


Example code:

#include <iostream>

class TestTry
{
public:
    // if you include the try after the method
    // it is caught below, but if you forget it is not
    void FunctionCatch()  /*** No try here! ***/
    {
        std::cout << "FunctionCatch" << std::endl;
        throw 42;
        std::cout << "FunctionCatch post-throw" << std::endl;
    }
    // what is this catch doing here?
    // shouldn't this be a compile error?
    catch (...)
    {
        std::cout << "FunctionCatch caught!" << std::endl;
    }
};

int main(int argc, char *argv[])
{
    TestTry Object;

    try
    {
        Object.FunctionCatch();
    }
    catch (...)
    {
        std::cout << "caught in main" << std::endl;
    }
}

> g++ -Wall TestTry.cpp
> ./a.out 
FunctionCatch
caught in main

Reply via email to