Why does the try statement (and catch clause) require a block?

R

reycri

While the following is allowed:

if (a == b)
SomeFunction();
else
OtherFunction();

The following is not:

try
DoSomething();
catch (Exception e)
ProcessError(e);

I checked the C# grammar and it confirms that, unlike all other
compound statements, it requires a block - ie: the opening { and
closing } are required.
I just want to be able to write the following (to avoid too many
indents):

try
using (FileStream file = new FileStream("someFile.txt",
FileMode.OpenOrCreate))
using (SomeResource rsrc = new SomeResource())
{
// do stuff here
}
catch (Exception e)
{
// handle error here
}
 
J

Jon Shemitz

While the following is allowed:

if (a == b)
SomeFunction();
else
OtherFunction();

The following is not:

try
DoSomething();
catch (Exception e)
ProcessError(e);

My best guess (and it IS just a guess) is that try blocks are not
free, and the language designers wanted them to stand out.
 
C

Carl Daniel [VC++ MVP]

Jon said:
My best guess (and it IS just a guess) is that try blocks are not
free, and the language designers wanted them to stand out.

In addition, both the try block and the catch block by necessity constitute
unique scopes - variables declared inside are not visible outside (nor would
they be if a single statement were allowed). For that reason, it makes
sense to require the braces to make it a formal block.

-cd
 
G

Guest

IMHO, I believe this is a combination of style and history. Style is
subjective and I don't take a side one way or the other as to whether it is
correct or not. However, one of the features of C# is that it is an
evolution of the C and C++ family of languages. Since the C++ try block has
curly braces, C# makes the migration path easier for the C++ programmer.
You'll find that C# syntax is similar in many ways to C++.

That said, I don't think there is a technical reason requiring that C# be
designed that way because VB.NET try blocks don't have begin/end, which is an
example of syntax that works without it.

Joe
 
S

Stephany Young

Not correct Joe. Vb.Net does indeed require an 'End Try' for every
Try/Catch/Finally construct.

In addition, like C# variable can have block scope with a Try section, a
Catch section or a Finally section, which is why, in C#, 'blocking' is
required.
 
J

Jon Shemitz

Stephany said:
In addition, like C# variable can have block scope with a Try section, a
Catch section or a Finally section, which is why, in C#, 'blocking' is
required.

A rather strong statement, that should probably have been qualified
with an "imho."

After all, a try statement doesn't *have* to have block-local
variables, and there wouldn't have been anything unusual in supporting
simple statements for try blocks but requiring compound statements if
you wanted block-local variables.

Similarly, while a `catch` statement *can* have a block local
exception variable, there wouldn't have been anything inconsistent
with making that local to the `catch` statement, whether that was
simple or compound. Look, for example, at `using` statements, `for`
loops, and `foreach` loops, each of which can declare a
statement-local variable (indeed, `foreach` loops *must* declare a
statement-local variable) while still taking both simple and compound
statements.

Iow, try statements are a C# inconsistency that can't really be
explained by invoking block locality.
 
J

Jon Skeet [C# MVP]

While the following is allowed:

if (a == b)
SomeFunction();
else
OtherFunction();

The following is not:

try
DoSomething();
catch (Exception e)
ProcessError(e);

I checked the C# grammar and it confirms that, unlike all other
compound statements, it requires a block - ie: the opening { and
closing } are required.
I just want to be able to write the following (to avoid too many
indents):

If you've got too much indentation, that suggests you should consider
refactoring. Taking out indentation will make your code harder to
understand.

Personally, I think it's more of a pity that your first example *does*
compile than that the second one doesn't.
 
C

Cor Ligthert [MVP]

As seldom,

We agree. I don't like these legacy parts,

(as well not in VB.Net where it is the same before you think that it is
about C#).

:)

Cor
 
C

Chris Nahr

In addition, both the try block and the catch block by necessity constitute
unique scopes - variables declared inside are not visible outside (nor would
they be if a single statement were allowed). For that reason, it makes
sense to require the braces to make it a formal block.

You and Stephany Young have it backwards. A new nested scope is
defined by the braces, not by the keyword that comes before the
braces. There are plenty of C# keywords that are optionally followed
by braces -- if, else, catch, for, while -- and a new scope is opened
only if they are. It's the same in C and C++, by the way.
 
R

reycri

I found the answer to my own question here (from c#'s roots in c++):

http://groups.google.com/group/comp...ock+require+c++&rnum=2&hl=en#68a54d70f74b5093

To summarize:
--------------------------------------------
Suppose braces were not required:
try
try
foo();
catch(T t) { ... }
catch(U u) { ... }
....
A "catch" must be associated with some "try". In my example, I want
the second catch to be associated with the first try, as indicated
by the indentation. But since multiple catch blocks are allowed for
a try, the compiler would have to associate every catch with the
nearest try.
 
J

Jon Shemitz

A "catch" must be associated with some "try". In my example, I want
the second catch to be associated with the first try, as indicated
by the indentation. But since multiple catch blocks are allowed for
a try, the compiler would have to associate every catch with the
nearest try.

Oh, duh! That makes perfect sense.

It doesn't explain why catch and finally blocks must have braces, but
I guess code like

try
{
This();
}
finally
That();

would seem really weird.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top