java异常

  • 分类:throwable的子类
    有 Error 和 Exception
  1. Error:一般是指与虚拟机相关的问题,如系统崩溃,虚拟机错误,内存空间不足,方法调用栈溢等。对于这类错 误的导致的应用程序中断,仅靠程序本身无法恢复和和预防,遇到这样的错误,建议让程序终止。
/**
 * An {@code Error} is a subclass of {@code Throwable}
 * that indicates serious problems that a reasonable application
 * should not try to catch. Most such errors are abnormal conditions.
 * The {@code ThreadDeath} error, though a "normal" condition,
 * is also a subclass of {@code Error} because most applications
 * should not try to catch it.
 * 

* A method is not required to declare in its {@code throws} * clause any subclasses of {@code Error} that might be thrown * during the execution of the method but not caught, since these * errors are abnormal conditions that should never occur. * * That is, {@code Error} and its subclasses are regarded as unchecked * exceptions for the purposes of compile-time checking of exceptions.

  1. Exception:表示程序可以处理的异常,可以捕获且可能恢复。遇到这类异常,应该尽可能处理异常,使程序恢复运行,而不应该随意终止异常。
**
 * The class {@code Exception} and its subclasses are a form of
 * {@code Throwable} that indicates conditions that a reasonable
 * application might want to catch.
 *
 * 

The class {@code Exception} and any subclasses that are not also * subclasses of {@link RuntimeException} are checked * exceptions. Checked exceptions need to be declared in a * method or constructor's {@code throws} clause if they can be thrown * by the execution of the method or constructor and propagate outside * the method or constructor boundary.


  • 其中Exception可分为2种:
  1. RuntimeException:运行期异常,系统异常
    可以处理也可以不处理,编译器不强制用 try..catch 处理或用 throws 声明,所以系统异常也称为 unchecked 异常。
    eg:NullPointerException,IndexOutOfBoundsException,ClassCastException,ClassNotFoundException , IllegalArgumentException,UnsupportedOperationException。

  2. CheckedException: 也成编译器异常
    编译器强制普通异常必须 try..catch 处理或用 throws 声明继 续抛给上层调用方法处理
    eg:CloneNotSupportedException

你可能感兴趣的:(java异常)