473,405 Members | 2,373 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,405 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 1428
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.