Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / General / August 2005

Tip: Looking for answers? Try searching our database.

Why does try/catch require {}

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John Bailo - 30 Aug 2005 19:28 GMT
how come I can say:

if() statement;  else statement;

but I cannot say

try statment; catch() statement;

I always have to use {}
Marina - 30 Aug 2005 19:32 GMT
Because it is less often that a try or a catch would have only 1 statement
in it. Typically, it will have many statements in it (especially the Try).
Whereas with 'if' statement, having only one statement you want to execute
is very common.

The actual reason is probably not having to do extra work with building it
in to the compiler. They would have spent extra time on something that
wasn't going to be used often.

Is it really a big deal to put the {} in there?

> how come I can say:
>
[quoted text clipped - 5 lines]
>
> I always have to use {}
Jon Shemitz - 30 Aug 2005 19:46 GMT
> how come I can say:
>
[quoted text clipped - 5 lines]
>
> I always have to use {}

I've wondered about that, too. Seems like a fairly large
inconsistency, and one that grates every time I write a one statement
catch or finally block. Just makes the code take more screen space.

Laziness seems the most likely answer. My best guess is that somehow
it's significantly easier to create the try table (in the metadata)
and to emit the special leave instructions when you have the { and }
tokens as a trigger than if you have to do this for a simple
statement.

But I suppose it's also possible that 'try statement catch
exception-block' creates some parser ambiguities that 'try
compound-statement catch exception-block' does not. I haven't actually
looked at a formal grammar for C# ....

Signature

www.midnightbeach.com

gmiley - 30 Aug 2005 20:13 GMT
I believe that it is best-practice to use {} for any code block
regardless of its length.

We have made that part of our coding standards here.

If you are just coding for yourself, that is a different situation, do
it however you like, but when several people are working in the same
code then requiring each if(), for(), while(), foreach(), et al use the
formatting of:

if (expression)
{
 // do this
}

... ends up making it easier for everyone involved.

As for the technical side of why they require {} for try/catch, well -
not a clue here.

I just know that I would hate to see:

try System.Data.DataRow = dataTable.Rows[0];
catch(System.Data.DataException ex) HandleError(ex); finally
dataTable.Dispose();
Jon Shemitz - 30 Aug 2005 21:17 GMT

> I believe that it is best-practice to use {} for any code block
> regardless of its length.

I know that a lot of people agree with you.

Obviously, I don't. I think that turning one line into three just adds
bulk and cuts clarity. I don't buy the "argument from maintainability"
- it's not at all hard to add the {} as needed and, in the VS
environment (ie, with a nice auto-formatter) it's all but impossible
to misread

 if (tst)
   statement1;
 statement2;

> We have made that part of our coding standards here.

How ... special. Does that have anything to do with try/catch syntax?

Signature

www.midnightbeach.com

Helge Jensen - 31 Aug 2005 16:39 GMT
/me bears wood to the stake and looks for matches ;)

Signature

Helge Jensen
  mailto:helge.jensen@slog.dk
  sip:helge.jensen@slog.dk
               -=> Sebastian cover-music: http://ungdomshus.nu <=-

gmiley - 31 Aug 2005 18:57 GMT
> We have made that part of our coding standards here.

How ... special. Does that have anything to do with try/catch syntax?

---

Not directly - no, except that in our case where it's required to use
{} for all code blocks, questions such as the original "Why does
try/catch require {}" do not exist.

---

I don't buy the "argument from maintainability"
- it's not at all hard to add the {} as needed and, in the VS
environment (ie, with a nice auto-formatter) it's all but impossible
to misread

 if (tst)
   statement1;
 statement2;

---

Buy the argument or not, but when you have to go through 10,000 lines
of code littered with evaluation/flow-control statements that do not
use {} and find that you need to add more instructions to those blocks,
you will be pounding your head wishing it had been done in the first
place.

I agree, VS does a nice job at formatting, but as general practice
(language/environment/etc non-specific) you cannot write code, as a
team-member, that relies on the IDE you prefer to use.

It's not just a matter of readability, or maintainability - it's a
matter of propper programming practices in a multi-author environment.

I'm not telling anyone they HAVE to do it a certain way - I was simply
stating my opinion on how to make the argument moot. Create standards
to be adhered to, one of which was the requirement of {} for all
code-blocks regardless of how many lines it might be at the time.
Bruno Jouhier - 30 Aug 2005 21:51 GMT
because the Java try/catch requires braces and C# copied it.
This does not explain why the Java try/catch requires braces, though.

Bruno

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.