Thoughts from the office by Ed Ball
Thursday, August 21, 2003

Is there a really good reason that the try statement (in C++, C#, JavaScript, etc.) and its friends catch and finally require a brace-enclosed block? What's wrong with this?

try
DoSomething();
catch (x)
LogError(x);

I know that the grammar doesn't allow it; I just wonder why. I suppose I could scour the newsgroups for the answer. Maybe I should ask on the proposed comp.std.csharp newsgroup.

8/21/2003 12:32:13 PM (Pacific Standard Time, UTC-08:00) | Comments [2] | Code#
3/4/2004 3:05:53 PM (Pacific Standard Time, UTC-08:00)
Hi Ed!

I think that all code blocks should be requied to be explicitly brace scoped.(if, do/while, try/catch, etc.) It just makes things more clear after midnight.

try Do()
catch(x) try DoDifferently();
catch(y) Log();
catch(z) Barf();

So the question is who thows z ( a variation of "who's on first(./?)" )? Do(), DoDifferently(), maybe both? It's not clear without explicit scoping.
Jon Pickett
3/4/2004 3:43:23 PM (Pacific Standard Time, UTC-08:00)
Hey, Jon!

I'm afraid I can't agree -- I love the fact that I can write a simple 'if' or 'if-else-if-else...' statement without extra braces to bloat and obscure the code.

Certainly you shouldn't put any complex statement inside an if, else, try, catch, or anything else without using brackets, for the very reason that you mention, but that shouldn't preclude me from using "implicit" braces where it enhances readability.
Ed
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Search
Archive
Links
Categories
Administration
Blogroll