Why Do try ... catch Blocks Require Braces?

Posted by Bidou on Stack Overflow See other posts from Stack Overflow or by Bidou
Published on 2010-06-09T18:57:09Z Indexed on 2010/06/10 11:12 UTC
Read the original article Hit count: 132

Filed under:

Hello.

While in other statements like if ... else you can avoid braces if there is only one instruction in a block, you cannot do that with try ... catch blocks: the compiler doesn't buy it. For instance:

try
    do_something_risky();
catch (...)
    std::cerr << "Blast!" << std::endl;

With the code above, g++ simply says it expects a '{' before do_something_risky(). Why this difference of behavior between try ... catch and, say, if ... else ?

Thanks!

© Stack Overflow or respective owner

Related posts about c++