[2392] in java-interest

home help back first fref pref prev next nref lref last post

Re: throws declataration in Java/beta

daemon@ATHENA.MIT.EDU (Jim Graham)
Sat Sep 30 07:38:59 1995

Date: Thu, 28 Sep 1995 11:53:29 -0700
From: flar@bendenweyr@Sun.COM (Jim Graham)
To: garya@village.org
Cc: java-interest@java@Sun.COM


> >Exceptions affect the control flow of the caller. While implementing
> >this feature we found a number of bugs in HotJava were we simply didn't
> >realize that a certain exception could thrown.
> 
> In other words, because the particular piece of code you were working on
> did not properly handle all the possible exceptions in the code it called,
> and this caused some (understandable :-)) debug problems, the decision was
> made to disallow throwing an exception of a different type.

No.  In other words, if exceptions are part of an API, then overridden
methods must operate within that specified API and must throw only
subclasses of the superclass's declared exceptions.  That keeps the API
clean.

On a side note - not having anything to do with the natural decision to
restrict overriding methods to only throw subclasses - an anecdote as
to the developmental value of declared exceptions is that while
cleaning up the warnings in the HotJava code we found cases where we
never bothered to catch some rather important exceptions which would
have shown up as bugs later.

> This seems like the wrong approach.  Wouldn't it be much more useful to
> require the equivalent of a C++/C switch stmt "default" catch clause in
> all exception situations?  That would allow for a subclass to throw and
> catch additional exceptions, and also require that the caller be prepared
> to at least be made aware that some unforseen exception has occurred and
> was not handled.

Only forseen exceptions are generally required to be declared (and
therefore need to be caught or explicitly passed on).  Exceptions which
are subclasses of RuntimeException or Error - which are not generally
part of the API of a given method - don't have to be declared.
Exceptions such as IOException, which are part of the natural working
domain of many methods, must be declared where they can occur so that
callers of that method are aware that the indicated exception is a
possible result of calling the method.

				...jim
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post