您的位置:首页 > 编程语言 > Java开发

Java基础知识整理(一)- Error和Exception

2013-11-03 15:51 591 查看

1. 共同点

        Error和Exception都继承自Throwable类

/*
* @(#)Exception.java	1.32 05/11/17
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/

package java.lang;

/**
* 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);
}
}


/*
* @(#)Error.java	1.17 05/11/17
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/

package java.lang;

/**
* An <code>Error</code> is a subclass of <code>Throwable</code>
* that indicates serious problems that a reasonable application
* should not try to catch. Most such errors are abnormal conditions.
* The <code>ThreadDeath</code> error, though a "normal" condition,
* is also a subclass of <code>Error</code> because most applications
* should not try to catch it.
* <p>
* A method is not required to declare in its <code>throws</code>
* clause any subclasses of <code>Error</code> that might be thrown
* during the execution of the method but not caught, since these
* errors are abnormal conditions that should never occur.
*
* @author  Frank Yellin
* @version 1.17, 11/17/05
* @see     java.lang.ThreadDeath
* @since   JDK1.0
*/
public class Error extends Throwable {
static final long serialVersionUID = 4980196508277280342L;

/**
* Constructs a new error 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 Error() {
super();
}

/**
* Constructs a new error 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 Error(String message) {
super(message);
}

/**
* Constructs a new error 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 error'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 Error(String message, Throwable cause) {
super(message, cause);
}

/**
* Constructs a new error 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 errors that are little more than
* wrappers for other throwables.
*
* @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 Error(Throwable cause) {
super(cause);
}
}


2. 不同点

 Exception(异常):

        在Java中程序的错误主要是语法错误和语义错误,一个程序在编译和运行时出现的错误我们统一称之为异常,它是VM(虚拟机)通知你的一种方式,通过这种方式,VM让你知

道,你(开发人员)已经犯了个错误,现在有一个机会来修改它。Java中使用异常类来表示异常,不同的异常类代表了不同的异常。但是在Java中所有的异常都有一个基类,叫做

Exception。

        1. 可以是“可控制的(checked)”或“不可控制的(unchecked)”

        2. 表示一个由程序员导致的错误

        3. 应该被应用程序级被处理

Error(错误):

        它指的是一个合理的应用程序不能截获的严重的问题。大多数都是反常的情况。”,错误是VM的一个故障(虽然它可以是任何系统级的服务)。所以,错误是很难处理的,一般的

开发人员(当然不是你)是无法处理这些错误的。比如内存溢出。

        1. 总是“不可控制的(unchecked)”

        2. 经常用来表示系统级错误或者底层资源的错误

        3. 如果可能的话,应该在系统级被捕捉

(注:参考文章--http://mousepc.iteye.com/blog/1279559

3. 异常的分类

        Exception分为2大类:运行时异常(RuntimeException / Unchecked Exception)和非运行时异常(Checked Exception)。

运行时异常:RuntimeException类直接继承自Exception类,称为运行时异常。Java中所有的运行时异常都直接或间接的继承自RuntimeException.

非运行时异常:Java中凡是继承自Exception但不是继承自RuntimeException的类都是非运行时异常

附:

error和exception有什么区别

error 表示恢复不是不可能但很困难的情况下的一种严重问题。比如说内存溢出。不可能指望程序能处理这样的情况。

exception 表示一种设计或实现问题。也就是说,它表示如果程序运行正常,从不会发生的情况。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: