checked Exception, unchecked Exception and Error

[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.如何可能的话,应该在系统级被捕捉
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值