Best practices for declared exceptions

S

Sigfried

Hi,
is there any pros to declare unchecked exceptions ? I'm asking since
there is no warning in eclipse for this case.

I'm asking for a rational, or better, an example of something which
can't be done whitout declaring some unchecked exception (i don't want
to start a troll).
 
M

Mike Schilling

Sigfried said:
Hi,
is there any pros to declare unchecked exceptions ? I'm asking since
there is no warning in eclipse for this case.

I'm asking for a rational, or better, an example of something which
can't be done whitout declaring some unchecked exception (i don't
want
to start a troll).

It's irrelevant to the compiler and JVM whether you declare an
unchecked exception or not. Either way, the method can throw the
exception or not, as it chooses, and either way, a method that calls
that method can catch that unchecked exception or not, as it chooses.

You might choose to declare an unchecked exception (and explain in
Javadoc why it gets thrown) as documentation, and there's nothing
wrong with doing so..
 
S

Stefan Ram

Sigfried said:
declare unchecked exceptions

Bugs in the client code have to be avoided at write time,
not caught at run time. Therefore, exceptions caused
by such bugs usually are runtime (=unchecked) exceptions.
They are used for events the client can not do anything
about, and intended to be caught by some top-level layer.

A hard disk error can not be avoided at write time, so
it must be handled by the client at run time. To enforce
this, one uses checked exceptions.
 
M

Mark Space

Sigfried said:
Hi,
is there any pros to declare unchecked exceptions ? I'm asking since
there is no warning in eclipse for this case.

I'm asking for a rational, or better, an example of something which
can't be done whitout declaring some unchecked exception (i don't want
to start a troll).


Just kind of speculating out loud, if you expressly throw any unchecked
exceptions, just document them. You don't need to add them to the
throws clause.

Other unchecked exceptions might be thrown by methods you call, so
unless your method is a leaf method it doesn't seem practical to try to
document all unchecked exceptions.
 
A

Arne Vajhøj

Sigfried said:
is there any pros to declare unchecked exceptions ? I'm asking since
there is no warning in eclipse for this case.

I'm asking for a rational, or better, an example of something which
can't be done whitout declaring some unchecked exception (i don't want
to start a troll).

You want to confuse the maintenance programmer and have him spend time
trying to figure out what the purpose is ?

:)

Besides that I can not think of any reasons.

Arne
 
S

Sigfried

Mark Space a écrit :
Just kind of speculating out loud, if you expressly throw any unchecked
exceptions, just document them. You don't need to add them to the
throws clause.

Other unchecked exceptions might be thrown by methods you call, so
unless your method is a leaf method it doesn't seem practical to try to
document all unchecked exceptions.

Ok, because of the recursion problem, you can't document all uncheked
exception. Is there a tool which can do that for me ? (i've tried
googling, but nothin came out).
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top