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 }
[quoted text, click to view] reycri@gmail.com wrote: > 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. -- ..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
[quoted text, click to view] Jon Shemitz wrote: > reycri@gmail.com wrote: > >> 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.
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
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] "reycri@gmail.com" wrote: > 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 > } >
[quoted text, click to view] Stephany Young wrote: > 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. -- ..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
In that paragraph, the 2nd 'C#' should have read 'VB.NET'. [quoted text, click to view] "Jon Shemitz" <jon@midnightbeach.com> wrote in message news:456935A8.7EA16CAC@midnightbeach.com... > Stephany Young wrote: > >> 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. > > -- > > .NET 2.0 for Delphi Programmers > www.midnightbeach.com/.net > What you need to know.
[quoted text, click to view] <reycri@gmail.com> wrote: > 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. -- Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
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] > <reycri@gmail.com> wrote: >> 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. > > -- > Jon Skeet - <skeet@pobox.com> > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet > If replying to the group, please do not mail me too
On Sat, 25 Nov 2006 20:36:25 -0800, "Carl Daniel [VC++ MVP]" [quoted text, click to view] <cpdaniel_remove_this_and_nospam@mvps.org.nospam> wrote: >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. --
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] "Joe Mayo (C# MVP)" <JoeMayoCMVP@discussions.microsoft.com> wrote in message news:61EED912-6C33-426B-B3ED-498FF831CA45@microsoft.com... > 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 > > > "reycri@gmail.com" wrote: > >> 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 >> } >> >>
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.
[quoted text, click to view] reycri@gmail.com wrote: > 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. -- ..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
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] "reycri@gmail.com" wrote: > 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. >
Don't see what you're looking for? Try a search.
|