Today I suddently be confused with the Exception and Error of Java. So I checked some infos about this.
The Java Language Specification says that, 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. The Java API defines a number of exception classes, both checked and unchecked. Additional exception classes, both checked and unchecked, may be declared by programmers.
The checked exception classes named in the throws clause are part of the contract between the implementor and user of the method or constructor. The throws clause of an overriding method may not specify that this method will result in throwing any checked exception which the overridden method is not permitted, by its throws clause, to throw. When interfaces are involved, more than one method declaration may be overridden by a single overriding declaration. In this case, the overriding declaration must have a throws clause that is compatible with all the overridden declarations.
We often encounter is the Exception, not the Error. Both of them extends the class Throwable. The documentation says that the class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch. A little example referenced from the book Thinking in Java lies below.
-----------------------------------------------------
package exceptions;
class SimpleException extends Exception {
}
public class InheritingExceptions {
public void f() throws SimpleException {
System.out.println("Throw SimpleException from f()");
throw new SimpleException();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
InheritingExceptions sed = new InheritingExceptions();
try {
sed.f();
} catch (SimpleException e) {
System.out.println("Caught it!");
}
System.out.println("the program continuse after the try bloch");
}
}
-----------------------------------------------------
The Error in Java program is the unchecked Exceptions. An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most applications should not try to catch it. A method is not required to declare in its throws clause any subclasses of Error that might be thrown during the execution of the method but not caught, since these errors are abnormal conditions that should never occur.
(some contents are referenced from Java Language Specification, jdk6.0 documentation, Thinking in java 4th)