2

Possible Duplicate:
Difference between try-catch syntax for function

There is this code:

int fun() try
{
    throw 1;
    return 2;
}
catch(int){
    return 1;
}

int fun2(){
    try{
        throw 1;
        return 2;
    }
    catch(int){
        return 1;
    }
}

What is purpose of keeping whole function body in try block and are these two definitions of functions different according to try..catch block?

Community
  • 1
  • 1
scdmb
  • 15,091
  • 21
  • 85
  • 128
  • What is your question? Please clarify. – Marcin Jan 06 '12 at 11:50
  • 1
    It's about keeping body of function in try..catch block (first function in example). What is this is used for and what is the difference according to second function in example? – scdmb Jan 06 '12 at 11:53
  • Those two functions are essentially identicial, it's simply relying on the fact that much like all c++ constructs (if/while etc) you don't need to specify braces if you're just doing one thing at that scope level. That being said, the first example is ugly as hell and I wouldn't do it. – Benj Jan 06 '12 at 11:54
  • `fun()` is so ugly and not fun. – karlphillip Jan 06 '12 at 11:58
  • @Benj: That's not true; a function-body can only be either a function-try-block (like the first example), or a compound-statement (i.e. a bunch of statements surrounded by `{}` like the second example). Something like `int fun() return 0;` with no braces is not allowed. – Mike Seymour Jan 06 '12 at 12:50
  • @MikeSeymour: You're quite correct, my apologies. I've always thought it was a bit nasty so I've never tried all the permutations... – Benj Jan 06 '12 at 13:21

0 Answers0