Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Why braces around try/catch? (http://www.velocityreviews.com/forums/t125292-why-braces-around-try-catch.html)

Henrik S. Hansen 08-06-2003 01:40 AM

Why braces around try/catch?
 
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


Thomas G. Marshall 08-06-2003 02:30 AM

Re: Why braces around try/catch?
 
Henrik S. Hansen <hsh@freecode.dk> 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.




Roedy Green 08-06-2003 03:33 AM

Re: Why braces around try/catch?
 
On Wed, 06 Aug 2003 03:40:26 +0200, "Henrik S. Hansen"
<hsh@freecode.dk> 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.

Shripathi Kamath 08-06-2003 07:04 AM

Re: Why braces around try/catch?
 
"Henrik S. Hansen" <hsh@freecode.dk> wrote in message
news:bgpmaa$1in7$1@news.cybercity.dk...
> 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



Jacob 08-06-2003 07:23 AM

Re: Why braces around try/catch?
 
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.


Jacob 08-06-2003 07:29 AM

Re: Why braces around try/catch?
 
Roedy Green wrote:
> On Wed, 06 Aug 2003 03:40:26 +0200, "Henrik S. Hansen"
> <hsh@freecode.dk> 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.



Jacob 08-06-2003 07:45 AM

Re: Why braces around try/catch?
 
Thomas G. Marshall wrote:
> Henrik S. Hansen <hsh@freecode.dk> 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);



Tor Iver Wilhelmsen 08-06-2003 07:53 AM

Re: Why braces around try/catch?
 
"Henrik S. Hansen" <hsh@freecode.dk> 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.


Lee Fesperman 08-06-2003 08:43 AM

Re: Why braces around try/catch?
 
Tor Iver Wilhelmsen wrote:
>
> "Henrik S. Hansen" <hsh@freecode.dk> 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)

Henrik S. Hansen 08-06-2003 09:32 AM

Re: Why braces around try/catch?
 
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



All times are GMT. The time now is 09:46 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.