CLASSES
Method Throws
8.4.4
See 17 for more discussion of threads and locks.
8.4.4 Method Throws
A
throws clause
is used to declare any checked exceptions ( 11.2) that can result
from the execution of a method or constructor:
Throws:
throws
ClassTypeList
ClassTypeList:
ClassType
ClassTypeList
,
ClassType
A compile time error occurs if any
ClassType
mentioned in a
throws
clause is not
the class
Throwable
( 20.22) or a subclass of
Throwable
. It is permitted but not
required to mention other (unchecked) exceptions in a
throws
clause.
For each checked exception that can result from execution of the body of a
method or constructor, a compile time error occurs unless that exception type or a
superclass of that exception type is mentioned in a
throws
clause in the declara
tion of the method or constructor.
The requirement to declare checked exceptions allows the compiler to ensure
that code for handling such error conditions has been included. Methods or con
structors that fail to handle exceptional conditions thrown as checked exceptions
will normally result in a compile time error because of the lack of a proper excep
tion type in a
throws
clause. Java thus encourages a programming style where
rare and otherwise truly exceptional conditions are documented in this way.
The predefined exceptions that are not checked in this way are those for which
declaring every possible occurrence would be unimaginably inconvenient:
Exceptions that are represented by the subclasses of class
Error
, for example
OutOfMemoryError
, are thrown due to a failure in or of the virtual machine.
Many of these are the result of linkage failures and can occur at unpredictable
points in the execution of a Java program. Sophisticated programs may yet
wish to catch and attempt to recover from some of these conditions.
The exceptions that are represented by the subclasses of the class
RuntimeException
, for example
NullPointerException
, result from
runtime integrity checks and are thrown either directly from the Java program
or in library routines. It is beyond the scope of the Java language, and perhaps
beyond the state of the art, to include sufficient information in the program to
reduce to a manageable number the places where these can be proven not to
occur.
163
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved