[2360] in java-interest
Re: throws declataration in Java/beta
daemon@ATHENA.MIT.EDU (Jim Graham)
Fri Sep 29 19:34:40 1995
Date: Fri, 29 Sep 1995 14:08:03 -0700
From: flar@bendenweyr.Eng.Sun.COM (Jim Graham)
To: garya@village.org
Cc: java-interest@java.Eng.Sun.COM
Gary,
Please understand two things:
1) Not all exceptions are candidates for declaration in Java.
Specifically, subclasses of RuntimeException and Error do not
need to be declared (and would probably generate a warning if
you tried to declare them). Only a specific subset of
exceptions that represent useful information about the normal
everyday functioning of the method need to be declared and
these exceptions should not be a subclass of either of the two
classes mentioned above.
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.
If you want to write a method that delegates its actions to an
arbitrary proxy to accomplish its functionality, then you can choose to
either promise that the proxies will throw only a predictable subset of
exceptions if they want to indicate a normal operating condition (i.e.
a non-runtime or non-error exception) or you can choose to make no
promises at all and declare that your method will throw Exception().
The latter would be much less useful than the former for the developers
who want to call your method.
Consider a declared exception as being sort of like a return code,
except that the mechanism of throwing it instead of returning it allows
batch handling of the particular condition by a single try-catch
wrapped around an arbitrarily complex block of processing rather than
requiring a test after each method call to see if the condition
occured. Only those exceptions that fit this model (i.e. they
represent a useful piece of information that requires "exceptional"
handling to deal with) should be declared. IOException is a typical
example.
...jim
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com