Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Why braces around try/catch?

Reply
Thread Tools

Why braces around try/catch?

 
 
Henrik S. Hansen
Guest
Posts: n/a
 
      08-06-2003
Does anyone know why braces are required around a try/catch block of
code? It seems counter-intuitive, since if/else etc. accept a single
statement without braces.

--
Henrik S. Hansen

 
Reply With Quote
 
 
 
 
Thomas G. Marshall
Guest
Posts: n/a
 
      08-06-2003
Henrik S. Hansen <(E-Mail Removed)> horrified us with:

> Does anyone know why braces are required around a try/catch block of
> code? It seems counter-intuitive, since if/else etc. accept a single
> statement without braces.



Ironically, I was just thinking about this the other day. I was musing
about simple uses like:

try {dangerMethod();} catch(Exception ignore){}

would be better as

try dangerMethod() IgnoreException; (new keyword)

But I was also wondering, as you are, about the simple but not allowed

try dangerMethod(); catch(Exception gripe) someStatement;

The conclusion I came to was that the try and catch are part of the same
construct. Unlike "else", a "catch" is mandatory. So perhaps they don't
want a statement terminator ( inbetween?

Not sure. Perhaps there is a parse-level reason for that.



 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      08-06-2003
On Wed, 06 Aug 2003 03:40:26 +0200, "Henrik S. Hansen"
<(E-Mail Removed)> wrote or quoted :

>Does anyone know why braces are required around a try/catch block of
>code? It seems counter-intuitive, since if/else etc. accept a single
>statement without braces.


It is probably because try catch are usually used over many lines of
code. For the few times you use over just one line, it would be
confusing. People would be expecting the {} to read.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Shripathi Kamath
Guest
Posts: n/a
 
      08-06-2003
"Henrik S. Hansen" <(E-Mail Removed)> wrote in message
news:bgpmaa$1in7$(E-Mail Removed)...
> Does anyone know why braces are required around a try/catch block of
> code? It seems counter-intuitive, since if/else etc. accept a single
> statement without braces.
>
> --
> Henrik S. Hansen
>


Pure speculation, and soapbox material:


Consider a single line if statement:

if (booleanexpression) statement1;

Equivalently, it can be written as

if (booleanexpression)
statement1;

Let us assume that the above is intuitive

if (booleanexpression)
statement1;
statement2;

is still valid, although it may not be what you intended.


Now carry over the same intuition to try and catch

If single statements were allowed

try
statement1;
catch (Exception e)
statement2;

should be intuitive

How about:

try
statement1;
statementx;
catch (Exception e)
statement2;
statementy;

Is statementy part of the catch? Intuitively? How about statementx being
part of the try? legal? intuitive? are the rules different for try and
catch?

Try nesting them


try
statement1;
statementx;
try
statement1a;
statementxa;
catch (Exception e)
statement2;
statementy;
statementz;
catch (Exception e)
statement2b;
statementyb;

Is statementz part of the catch or the outer try? if and else structures
with single statements have less or no ambiguity imho.

--
Shripathi Kamath


 
Reply With Quote
 
Jacob
Guest
Posts: n/a
 
      08-06-2003
Shripathi Kamath wrote:

> try
> statement1;
> statementx;
> catch (Exception e)
> statement2;
> statementy;


This whould be a syntax error equivalent to:

if (clause)
statement1;
statement2;
else
statements;

BTW: This topic was discussed awhile ago (with me
as the OP), and there was no clear conclusion why
you need braces around single statement try-catch
or methods.

The gereral opinion (of style) is that you should
not utilize the possibility to omit braces where
this is indeed possible (of which I disagree).

I think that the language whould be simpler if the
syntax was identical in all cases.

 
Reply With Quote
 
Jacob
Guest
Posts: n/a
 
      08-06-2003
Roedy Green wrote:
> On Wed, 06 Aug 2003 03:40:26 +0200, "Henrik S. Hansen"
> <(E-Mail Removed)> wrote or quoted :
>
>
>>Does anyone know why braces are required around a try/catch block of
>>code? It seems counter-intuitive, since if/else etc. accept a single
>>statement without braces.

>
>
> It is probably because try catch are usually used over many lines of
> code. For the few times you use over just one line, it would be
> confusing. People would be expecting the {} to read.


I find it hard to believe that James Gosling & Co. back in
the early 90's actually considered how many lines of code
they believed people would put within a try statement and
concluded "many" so they forced braces on the construct.

As a matter of style I actually put as little code as
possible within a try block in order to better indicate
where exactly an exception may occour. And from my
experience, a catch block very often is a one-liner.


 
Reply With Quote
 
Jacob
Guest
Posts: n/a
 
      08-06-2003
Thomas G. Marshall wrote:
> Henrik S. Hansen <(E-Mail Removed)> horrified us with:
>
>
>>Does anyone know why braces are required around a try/catch block of
>>code? It seems counter-intuitive, since if/else etc. accept a single
>>statement without braces.

>
>
>
> Ironically, I was just thinking about this the other day. I was musing
> about simple uses like:
>
> try {dangerMethod();} catch(Exception ignore){}
>
> would be better as
>
> try dangerMethod() IgnoreException; (new keyword)
>
> But I was also wondering, as you are, about the simple but not allowed
>
> try dangerMethod(); catch(Exception gripe) someStatement;
>
> The conclusion I came to was that the try and catch are part of the same
> construct. Unlike "else", a "catch" is mandatory. So perhaps they don't
> want a statement terminator ( inbetween?


Possibly. But for a do-statement, the while is mandatory,
but the braces are not:

do
statement;
while (clause);


 
Reply With Quote
 
Tor Iver Wilhelmsen
Guest
Posts: n/a
 
      08-06-2003
"Henrik S. Hansen" <(E-Mail Removed)> writes:

> Does anyone know why braces are required around a try/catch block of
> code? It seems counter-intuitive, since if/else etc. accept a single
> statement without braces.


Actually the real problem is the reverse case: Why does if, for, do
and while *bug-inducingly* allow single statements? The first edition
of Java in a Nutshell used try/catch examples without braces, so it
seems it was so at one point (pre-1.0.2).

They really should amend the language the other way, and force use of
braces for if, for, do and while as well as for the current switch,
synchronized, try, cath, finally, method declarations, etc.

 
Reply With Quote
 
Lee Fesperman
Guest
Posts: n/a
 
      08-06-2003
Tor Iver Wilhelmsen wrote:
>
> "Henrik S. Hansen" <(E-Mail Removed)> writes:
>
> > Does anyone know why braces are required around a try/catch block of
> > code? It seems counter-intuitive, since if/else etc. accept a single
> > statement without braces.

>
> Actually the real problem is the reverse case: Why does if, for, do
> and while *bug-inducingly* allow single statements? The first edition
> of Java in a Nutshell used try/catch examples without braces, so it
> seems it was so at one point (pre-1.0.2).
>
> They really should amend the language the other way, and force use of
> braces for if, for, do and while as well as for the current switch,
> synchronized, try, cath, finally, method declarations, etc.


My explanation is that the language designers allowed braceless if, for, etc. simply for
compatiblity with C/C++. For Java only constructs, they went with their preferences,
which were to require braces.

In that past, I have moved simple C functions directly to Java by adding public. So, I
found it convenient.

I'd never considered a braceless switch before. Here's one that actually does something:

switch (character)
case '\n' :
continue;

.... ;^)

--
Lee Fesperman, FirstSQL, Inc. (http://www.firstsql.com)
================================================== ============
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS (http://www.firstsql.com)
 
Reply With Quote
 
Henrik S. Hansen
Guest
Posts: n/a
 
      08-06-2003
Tor Iver Wilhelmsen wrote:
> They really should amend the language the other way, and force use of
> braces for if, for, do and while as well as for the current switch,
> synchronized, try, cath, finally, method declarations, etc.
>


I disagree strongly. Why on earth should you introduce redundant
syntax? Single-line if statements are easy to read, and never confusing
in my experience.

Java is already pretty bondage-and-discipline, why make it worse?

--
Henrik S. Hansen

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
warning: missing braces around initializer Pawel C++ 13 11-14-2008 12:08 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
allowing braces around suites Kjetil Torgrim Homme Python 99 09-08-2004 06:13 AM
Re: allowing braces around suites Michael Sparks Python 1 08-31-2004 02:43 AM
RE: allowing braces around suites Delaney, Timothy C (Timothy) Python 2 08-30-2004 03:39 PM



Advertisments