Groups | Blog | Home
all groups > c# > november 2006 >

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


reycri NO[at]SPAM gmail.com
11/25/2006 7:11:02 PM
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
}
Jon Shemitz
11/25/2006 7:58:58 PM
[quoted text, click to view]

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.

--

..NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
Carl Daniel [VC++ MVP]
11/25/2006 8:36:25 PM
[quoted text, click to view]

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

Joe Mayo (C# MVP)
11/25/2006 9:44:05 PM
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
--
http://www.csharp-station.com


[quoted text, click to view]
Jon Shemitz
11/25/2006 10:35:20 PM
[quoted text, click to view]

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.

--

..NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
Stephany Young
11/26/2006 12:00:00 AM
In that paragraph, the 2nd 'C#' should have read 'VB.NET'.


[quoted text, click to view]

Jon Skeet [C# MVP]
11/26/2006 12:00:00 AM
[quoted text, click to view]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Cor Ligthert [MVP]
11/26/2006 12:00:00 AM
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

"Jon Skeet [C# MVP]" <skeet@pobox.com> schreef in bericht
news:MPG.1fd35a402270d0498d660@msnews.microsoft.com...
[quoted text, click to view]

Chris Nahr
11/26/2006 12:00:00 AM
On Sat, 25 Nov 2006 20:36:25 -0800, "Carl Daniel [VC++ MVP]"
[quoted text, click to view]

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.
--
Stephany Young
11/26/2006 12:00:00 AM
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.


[quoted text, click to view]

reycri NO[at]SPAM gmail.com
11/26/2006 7:48:48 AM
I found the answer to my own question here (from c#'s roots in c++):

http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/a14a4ce80abd84c5/68a54d70f74b5093?lnk=st&q=try+catch+block+require+c%2B%2B&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.
--------------------------------------------

The key here is that a try statement can have more than one catch
clauses. In contrast, each if statement only has one else clause.
Jon Shemitz
11/26/2006 11:27:50 AM
[quoted text, click to view]

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.

--

..NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
Peter Bromberg [C# MVP]
11/26/2006 3:33:01 PM
Yep. If at first you don't succeed, try, try again.
:-)
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




[quoted text, click to view]
AddThis Social Bookmark Button