Java Reference
In-Depth Information
It is permitted but not required to mention unchecked exception classes (§ 11.1.1 ) in a throws
clause.
The relationship between a throws clause and the exception checking for a method or con-
structor body is specified in § 11.2.3 .
Essentially, for each checked exception that can result from execution of the body of
a method or constructor, a compile-time error occurs unless its exception type or a su-
pertype of its exception type is mentioned in a throws clause in the declaration of the
method or constructor.
The requirement to declare checked exceptions allows a Java compiler to ensure that
code for handling such error conditions has been included. Methods or constructors
that fail to handle exceptional conditions thrown as checked exceptions in their bodies
will normally cause compile-time errors if they lack proper exception types in their
throws clauses. The Java programming language thus encourages a programming style
where rare and otherwise truly exceptional conditions are documented in this way.
The relationship between the throws clause of a method and the throws clauses of overridden
or hidden methods is specified in § 8.4.8.3 .
8.4.7. Method Body
A method body is either a block of code that implements the method or simply a semicolon,
indicating the lack of an implementation.
MethodBody:
Block
;
It is a compile-time error if a method declaration is either abstract or native and has a block
for its body.
It is a compile-time error if a method declaration is neither abstract nor native and has a semi-
colon for its body.
If an implementation is to be provided for a method declared void , but the implement-
ation requires no executable code, the method body should be written as a block that
contains no statements: “ { } ”.
If a method is declared void , then its body must not contain any return statement (§ 14.17 )
that has an Expression , or a compile-time error occurs.
Search WWH ::




Custom Search