分类: Java
2010-07-14 11:08:58
Hist:
1007014, draft
When a program violates the semantic constraints of the Java programming language, the Java virtual machine signals this error to the program as an exception. An exception is said to be thrown from the point where it occurred and said to be caught at the point to which control is transferred.
Programs can also throw exceptions explicitly, using throw statements.
The Causes of Exceptions
Three reasons:
1) an abnormal execution condition was synchronously detected by the JVM.
2) a throw statement was executed.
3) an asynchronous exception occurred either because:
a) the (deprecated) method stop of class Thread was invoked
b) an internal error has occurred in the vm
Exceptions are represented by instances of the class Throwable and instances of its subclasses. These classes are, collectively, the exception classes.
Compile-Time Checking of Exceptions
A compiler for the Java programming language checks, at compile time, that a program contains handlers for checked exceptions, by analyzing which checked exceptions can result from execution of a method or constructor. For each checked exception which is a possible result, the throws clause for the method or constructor must mention the class of that exception or one of the superclasses of the class of that exception.
The unchecked exceptions classes are the class RuntimeException and its subclasses, and the class Error and its subclasses. All other exception classes are checked exception classes.
Exception Analysis of Expressions
Exception Analysis of Statements
Exception Checking
Why Errors are Not Checked
The unchecked exception classes which are the error classes are exempted from compile-time checking because they can occur at many points in the program and recovery from them is difficult or impossible. A program declaring such exceptions would be cluttered, pointlessly.
Why Runtime Exceptions are Not Checked
Because having to declare such exceptions would not aid significantly in establishing the correctness of programs. The information available to a compiler, and the level of analysis the compiler performs, are usually not sufficient to establish that such run-time exception cannot occur.
Handling of an Exception
A statement or expression is dynamically enclosed by a catch class if it appears within the try block of the try statement of which the catch clause is a part, or if the caller of statement or expression is dynamically enclosed by the catch clause.
Whether a particular catch clause handles an exception is determined by comparing the class of the object that was thrown to the declared type of the parameter of the catch clause.
Exceptions are Precise
Handling Asynchronous Exceptions
The Exception Hierarchy
Loading and Linkage Errors
The JVM throws an object that is an instance of a subclass of LinkageError.
Virtual Machine Errors
The JVM throws an object that is an instance of a subclass of VirtualMachineError.
REF
1. JLS 3nd