[2375] 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 (tmb@best.com)
Sat Sep 30 03:53:25 1995

From: tmb@best.com
Date: Fri, 29 Sep 1995 22:35:19 -0800
To: flar@bendenweyr.Eng.Sun.COM (Jim Graham)
Cc: garya@village.org, java-interest@java.Eng.Sun.COM
In-Reply-To: <9509292108.AA15842@bendenweyr.Eng.Sun.COM>
Reply-To: tmb@best.com

Jim Graham writes:
 > 	2) If you want to throw a non-runtime and non-error exception
 > 	that is not declared by a method that you are overriding then
 > 	you are violating the API of that method.  This is as strong a
 > 	violation as if you were trying to return a String from a
 > 	method that was declared as returning an int in your
 > 	superclass.  It violates the API and is not allowed.

Many APIs are correctly declared as:

	"method f can throw exactly the set of exceptions
        that method g of the object held in instance variable x can
        throw"

or

	"method f can throw exactly the set of exceptions
	that the method that overrides it throws"

The Java type system isn't powerful enough either to let the
programmer declare or to type check such an API.  That means that
"method f" in both cases has to be declared as "throws any", and
callers will have to be able to deal with any exception that comes
their way after all.

Type and exception declarations, in particular in OO languages, where
you have inheritance, are only a crude approximation to an API.  A lot
has to be specified in natural language and cannot be type checked.

Problems arise if "method f" accidentally gets declared as "throws
none".  This may look harmless at first, and people may not notice the
mistake for a long time.  But it is a serious blunder that is tedious
to work around and cannot be detected automatically at all.  "throws
none" is a dangerous default.

If you want exception declarations at all in a language like Java, the
default should probably be "throws any".  It keeps the unwary from
tripping over exception declarations or designing unusable APIs.
Fortunately, Java could change to such a default at any time without
breaking existing code, so it doesn't have to be decided right now.
Maybe, in a year or two, people will have run into enough problems
with this feature to change the default.

				Thomas.
-
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