For example:
if (true) try
{
// works as expected with both true and false, but is it legal?
}
catch (...)
{
// ...
}
In other words, is it legal to put the try-block right after the if condition?
The syntax of a try
block (which is a statement in C++) is
try compound-statement handler-sequence
And the syntax of if
is:
attr(optional) if ( condition ) statement_true
attr(optional) if ( condition ) statement_true else statement_false
where:
statement-true
- any statement (often a compound statement), which is executed if condition evaluates to true
statement-false
- any statement (often a compound statement), which is executed if condition evaluates to false
So yes, your code is legal code in C++
.
statement_true
in your case is a try
block.
In legality, it is similar to:
if (condition) for(...) {
...
}
But your code is not very readable and can be the victim of some C++ pitfalls when an else
is added. So, it is advisable to add explicit {...}
after if
in your case.
int x; int x;
is a simple example that's allowed by the grammar but illegal nonetheless.
statement_true
in your case is a try
block" lacks proof, because the quote does not say whether try-block is statement_true
or not. All it says if
block can be defined in terms of statement_true
, but it does not define what statement_true
is, does it? In that sense, your quote from the standard is meaningless and irrelevant to your claim because you did not connect all the mandatory text from the standard to back your claim.
is it legal to put the try-block right after the if condition?
It is legal. Your code is same as (and better to write as):
if (true) {
try
{
// works as expected with both true and false, but is it legal?
}
catch (...)
{
// ...
}
}
So if the condition is false
then the try-catch
block won't be executed. If this is what you expect, it's fine.
{
and }
braces, the equivalence becomes trivially obvious.
Mar 14, 2016 at 14:33
if
statement in the original question is exactly the same not counting spaces. Without braces, your formatting reveals that there's nothing "magical" in putting a try-catch block right after the if
statement.
Mar 14, 2016 at 15:02
Yes. The braces of an if
are optional. Imagine you have {}
around the try { .. } catch { .. }
.
It may interest you to know that this is what happens when you write if
/else if
/else
; C++ doesn't actually have else if
… so this:
if (A) {
}
else if (B) {
}
is actually parsed as this:
if (A) {
}
else
if (B) {
}
which is this:
if (A) {
}
else {
if (B) {
}
}
elseif
token such as can be found in some languages. The if
part of the else if
construct is a whole new statement, and this causes some interesting misunderstandings in code that doesn't recognize it as such.
It's well-formed. try-blocks are statements as per [stmt.stmt]/1, and statements are following if (…)
as per [stmt.select]/1.
if
statement. But why not just test the condition using try?int main() try {} catch(...){}
is also legal.class foo { int bar; foo() try : bar(3) { ... } catch ...
).