[b]1. checked exception[/b]
a. 都是Exception的子类,若子类可能抛出X异常,则父类也必须throws X异常,效率低,耦合度高。
b. 需要强制catch,否则编译器直接报错。
/**
* The class <code>Exception</code> and its subclasses are a form of
* <code>Throwable</code> that indicates conditions that a reasonable
* application might want to catch.
*
* @author Frank Yellin
* @version 1.32, 11/17/05
* @see java.lang.Error
* @since JDK1.0
*/
public class Exception extends Throwable {
static final long serialVersionUID = -3387516993124229948L;
/**
* Constructs a new exception with <code>null</code> as its detail message.
* The cause is not initialized, and may subsequently be initialized by a
* call to {@link #initCause}.
*/
public Exception() {
super();
}
/**
* Constructs a new exception with the specified detail message. The
* cause is not initialized, and may subsequently be initialized by
* a call to {@link #initCause}.
*
* @param message the detail message. The detail message is saved for
* later retrieval by the {@link #getMessage()} method.
*/
public Exception(String message) {
super(message);
}
/**
* Constructs a new exception with the specified detail message and
* cause. <p>Note that the detail message associated with
* <code>cause</code> is <i>not</i> automatically incorporated in
* this exception's detail message.
*
* @param message the detail message (which is saved for later retrieval
* by the {@link #getMessage()} method).
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
* @since 1.4
*/
public Exception(String message, Throwable cause) {
super(message, cause);
}
/**
* Constructs a new exception with the specified cause and a detail
* message of <tt>(cause==null ? null : cause.toString())</tt> (which
* typically contains the class and detail message of <tt>cause</tt>).
* This constructor is useful for exceptions that are little more than
* wrappers for other throwables (for example, {@link
* java.security.PrivilegedActionException}).
*
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
* @since 1.4
*/
public Exception(Throwable cause) {
super(cause);
}
}
[b]2. unchecked exception[/b]([color=green]RuntimeException继承Exception[/color])
a. 都是RuntimeException的子类。
b. 非必须catch,是无法预料的。比如,list..size(),若list=null, 则会抛出NullPointerException,该异常是一个RuntimeException, 也就是unchecked exception。
public class RuntimeException extends Exception
[b]3. Error[/b]
a. 都是Error的子类。
b. 非必须catch,是无法预料的。
[color=green]Error和Exception都继承自Throwable[/color]。不同处:
[b]Exceptions[/b]: 表示一种设计或实现问题。也就是说,它表示如果程序运行正常,从不会发生的情况。程序员可以对异常代码进行改正。
1.可以是[color=green]可控制的(checked)和不可控制的(unchecked)[/color]
2.表示一个由程序员导致的错误
3.应该在应用程序级被处理
[b]Errors[/b]: 表示恢复不是不可能但很困难,是一种严重问题,比如说内存溢出。
1.总是[color=green]不可控制的(unchecked)[/color]
2.经常用来用于表示系统错误或低层资源的错误
3.如何可能的话,应该在系统级被捕捉