473,386 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Why does try/catch require {}


how come I can say:
if() statement; else statement;

but I cannot say

try statment; catch() statement;
I always have to use {}

Aug 30 '05 #1
7 1427
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?

"John Bailo" <ja*****@texeme.com> wrote in message
news:2M*****************************************@s peakeasy.net...

how come I can say:
if() statement; else statement;

but I cannot say

try statment; catch() statement;
I always have to use {}

Aug 30 '05 #2
John Bailo wrote:
how come I can say:

if() statement; else statement;

but I cannot say

try statment; catch() statement;

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# ....

--

www.midnightbeach.com
Aug 30 '05 #3
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();

Aug 30 '05 #4
gmiley wrote:
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?

--

www.midnightbeach.com
Aug 30 '05 #5
because the Java try/catch requires braces and C# copied it.
This does not explain why the Java try/catch requires braces, though.

Bruno
Aug 30 '05 #6
/me bears wood to the stake and looks for matches ;)

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Aug 31 '05 #7
> 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.

Aug 31 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Pierre Rouleau | last post by:
The std::exception class defined in the Standard C++ <exception> header specifies that the constructors could throw any exception becuase they do not have a throw() specification. Why is that? ...
4
by: werdna.sivad | last post by:
In PHP I need to be able to execute another script from inside another and catch the output. How do I do this? The require() and include() functions will execute a script, but it sends the output...
7
by: John Bailo | last post by:
how come I can say: if() statement; else statement; but I cannot say try statment; catch() statement;
9
by: David Thielen | last post by:
Hi; I am sure I am missing something here but I cannot figure it out. Below I have a program and I cannot figure out why the xpath selects that throw an exception fail. From what I know they...
28
by: lovecreatesbeauty | last post by:
Besides printing out for example " a.out: p113.c:8: main: Assertion `0' failed. Aborted " and a switch option NDEBUG, what other benefits does assert() provide in any scope of designing,...
12
by: reycri | last post by:
While the following is allowed: if (a == b) SomeFunction(); else OtherFunction(); The following is not: try
10
by: Rahul | last post by:
Hi Everyone, I have the following exception class, class E1 { }; class E2 {
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.