what does this code fragment do

A

Andy Fish

Hi,

Whilst maintaining some old java code I have come across a pile of routines
that do this

public void someMethod() throws MyException {
try {
do some stuff...
} catch (MyException ex) {
throw ex;
}
}

I can't figure out what would make the programmer catch a specific exception
just to throw it again. Is there any legitimate functional or style reason
for the code to be like this or is it just a some legacy cruft?

Andy
 
Y

Yu SONG

Andy said:
Hi,

Whilst maintaining some old java code I have come across a pile of routines
that do this

public void someMethod() throws MyException {
try {
do some stuff...
} catch (MyException ex) {
throw ex;
}
}

I can't figure out what would make the programmer catch a specific exception
just to throw it again. Is there any legitimate functional or style reason
for the code to be like this or is it just a some legacy cruft?

Andy

I can't figure out either.
If he wants to throw the exception out, why does he use the "try" block
then?
 
W

Will

If, for example, the code within the try {} block opens a socket, the catch
block within the method may close that socket (and any associated
input/output streams) before manually throwing the exception... This way the
caller need not worry about closing the streams/socket if an exception
occurs.

Regards, Will.
 
G

Guest

Yu said:
I can't figure out either.
If he wants to throw the exception out, why does he use the "try" block
then?

Perhaps to be able to put a breakpoint in
throw ex;
using some debugger...

- Dario
 
C

Chris Uppal

Andy said:
public void someMethod() throws MyException {
try {
do some stuff...
} catch (MyException ex) {
throw ex;
}
}

I can't figure out what would make the programmer catch a specific
exception just to throw it again.

The only thing /I/ can think of is that it gives you somewhere to put
breakpoints in the debugger. Or perhaps a place to add temporary
logging without disturbing (re-indenting) too much code.

Cynically, though, I'd guess that it's really just mindless copy-and-paste
scarring.

-- chris
 
Y

Yu SONG

Will said:
If, for example, the code within the try {} block opens a socket, the catch
block within the method may close that socket (and any associated
input/output streams) before manually throwing the exception... This way the
caller need not worry about closing the streams/socket if an exception
occurs.

Regards, Will.

Oh yes, I agree.

But in this case, there is only one line which just throws the exception...
 
J

Joona I Palaste

Andy Fish said:
Whilst maintaining some old java code I have come across a pile of routines
that do this
public void someMethod() throws MyException {
try {
do some stuff...
} catch (MyException ex) {
throw ex;
}
}
I can't figure out what would make the programmer catch a specific exception
just to throw it again. Is there any legitimate functional or style reason
for the code to be like this or is it just a some legacy cruft?

In our code we routinely use things like this:

public void someMethod() throws MyException {
try {
/* do some stuff... */
}
catch (MyException ex) {
throw ex;
}
catch (Exception ex) {
throw new MyException(ex.getMessage());
}
}

This is handy when "do some stuff" can throw all kinds of Exceptions,
but we specifically want someMethod to only throw MyExceptions. Of
course the way your code sample is written, it's pointless.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"When a man talks dirty to a woman, that's sexual harassment. When a woman talks
dirty to a man, that's 14.99 per minute + local telephone charges!"
- Ruben Stiller
 
J

Jim McMaster

Will said:
If, for example, the code within the try {} block opens a socket, the catch
block within the method may close that socket (and any associated
input/output streams) before manually throwing the exception... This way the
caller need not worry about closing the streams/socket if an exception
occurs.

Regards, Will.
Of course, you could do the same thing in a finally clause without catching
the exception as well.
 
W

Will

Yu - If the catch{} block simply throws the exception, it *is* completely
pointless : )

Regards, Will.
 
W

Will

Andrew Thompson said:
Will, Yu! - If the ..
<http://www.physci.org/kbd.jsp?key=del>
key is not utilised posts become needlessly
long. This thread that started at 21 lines has
grown to 34, 55, 77 and last post *95* lines.

My apologies.

Talking of needlessly long posts, was it not possible to write 'del' instead
of a *37* character URL that lead to a page depicting simple information
that I already knew?

Regards, Will.
 
A

Andrew Thompson

Talking of needlessly long posts, was it not possible to write 'del' instead
of a *37* character URL that lead to a page depicting simple information
that I already knew?

It's more fun that way,
(shrugs) ..for me, anyway. :)

For further details, see..
[ No URL's, ..just this once ;-) ]
 
R

Rob Shepherd

consider adding a few lines such as the following...

public void someMethod() throws MyException {
try {
do some stuff...
} catch (MyException ex) {

if(ex.getMessage().equals("no tea and biscuits")
{
OtherClass.doSomeExceptionLogging(ex.getMessage())
YAOtherClass.doSomeGossip("Aparently someMethod() said \" no tea and biscuits\" ");
}

throw ex;
}
}

It may have been the programmers desire to do something like this
but never got round to it / forgot about it...

My 2c

Rob
--
------------------------------------------------------------------------
| Rob can be contacted privately through a temporary email account. |
| Courtesy of http://www.mailinator.com |
| |
| (e-mail address removed) |
| |
| This address will not be checked after May 31st 2004 |
------------------------------------------------------------------------
 
T

Tony Morris

public void someMethod() throws MyException {
try {
do some stuff...
} catch (MyException ex) {
throw ex;
}
}

This is functionally equivalent to:

public void someMethod() throws MyException {
do some stuff...
}

I can only guess that the developer had some additional code in the catch
block, which was removed to leave redundant code. The only other guess is a
lack of understanding of the Java exception handling mechanism.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
Home : +61 7 5502 7987
Work : +61 7 5552 4076
Mobile : 0408 711 099
(2003 VTR1000F)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top