10.12.2012 Views

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CLASSES Method Throws 8.4.6<br />

• If R 1 is void then R 2 is void.<br />

DISCUSSION<br />

<strong>The</strong> notion of return-type substitutability summarizes the ways in which return types may<br />

vary among methods that override each other.<br />

Note that this definition supports covariant returns - that is, the specialization of the<br />

return type to a subtype (but only for reference types).<br />

Also note that unchecked conversions are allowed as well. This is unsound, and<br />

requires an unchecked warning whenever it is used; it is a special allowance is made to<br />

allow smooth migration from non-generic to generic code.<br />

8.4.6 Method Throws<br />

A throws clause is used to declare any checked exceptions (§11.2) that can result<br />

from the execution of a method or constructor:<br />

Throws:<br />

throws ExceptionTypeList<br />

ExceptionTypeList:<br />

ExceptionType<br />

ExceptionTypeList , ExceptionType<br />

ExceptionType:<br />

ClassType<br />

TypeVariable<br />

A compile-time error occurs if any ExceptionType mentioned in a throws clause<br />

is not a subtype (§4.10) of Throwable. It is permitted but not required to mention<br />

other (unchecked) exceptions in a throws clause.<br />

For each checked exception that can result from execution of the body of a<br />

method or constructor, a compile-time error occurs unless that exception type or a<br />

supertype of that exception type is mentioned in a throws clause in the declaration<br />

of the method or constructor.<br />

<strong>The</strong> requirement to declare checked exceptions allows the compiler to ensure<br />

that code for handling such error conditions has been included. Methods or constructors<br />

that fail to handle exceptional conditions thrown as checked exceptions<br />

will normally result in a compile-time error because of the lack of a proper exception<br />

type in a throws clause. <strong>The</strong> <strong>Java</strong> programming language thus encourages a<br />

DRAFT<br />

221

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!