Java的异常处理机制概述

完美的程序也许只存在于想象之中。

java.lang 包下的 Throwable 类

java.lang

Class Throwable

java.lang.Object

        java.lang.Throwable

All Implemented Interfaces:

Serializable

Direct Known Subclasses:

ErrorException

public class Throwable

extends Object

implements Serializable

The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement. Similarly, only this class or one of its subclasses can be the argument type in a catch clause.

For the purposes of compile-time checking of exceptions,Throwable and any subclass of Throwable that is not also a subclass of either RuntimeException or Error are regarded as checked exceptions.


在 Java 语言中 Throwable 类是所有错误和异常的父类。只有它或者它的子类的实例对象才能被 JVM 或者 Java 的 throw 语句抛出。也只有它或者它的子类才能作为 catch 子句的参数类型。

为了在编译器检查异常,对于 Throwable 和它的任一子类,如果不是 RuntimeException 或者 Error 类的子类,都会被当作可检查异常(在源代码中必须显式的进行处理:捕获或者向上抛出)。

java.lang 包下的 Error 类

java.lang

Class Error

java.lang.Object

        java.lang.Throwable

                java.lang.Error

All Implemented Interfaces:

Serializable

Direct Known Subclasses:

AnnotationFormatErrorAssertionErrorAWTErrorCoderMalfunctionErrorFactoryConfigurationErrorFactoryConfigurationErrorIOErrorLinkageErrorSchemaFactoryConfigurationErrorServiceConfigurationErrorThreadDeathTransformerFactoryConfigurationErrorVirtualMachineError

public class Error

extends Throwable

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. That is, Error and its subclasses are regarded as unchecked exceptions for the purposes of compile-time checking of exceptions.


Error 类是 Throwable 类的子类,它表示严重的问题,一个合理的应用程序不应该试图捕获这些问题。大多数这样的错误是不正常的情况。ThreadDeath 错误尽管是一个“正常”情况,但因为大多数应用程序都不应该试图捕获它,所以它也是错误,是 Error 的子类。

对于任意在方法执行过程中可能会被抛出但未被捕获的错误的子类,该方法不需要在其 throws 子句中声明它们,因为这些错误都是不应该发生的不正常情况。也就是说,Error 和它的子类被视为 不检查异常,以便不影响编译器检查异常。

java.lang 包下的 Exception 类

java.lang

Class Exception

java.lang.Object

        java.lang.Throwable

                java.lang.Exception

All Implemented Interfaces:

Serializable

Direct Known Subclasses:

AclNotFoundExceptionActivationExceptionAlreadyBoundExceptionApplicationExceptionAWTExceptionBackingStoreExceptionBadAttributeValueExpExceptionBadBinaryOpValueExpExceptionBadLocationExceptionBadStringOperationExceptionBrokenBarrierExceptionCertificateExceptionCloneNotSupportedExceptionDataFormatExceptionDatatypeConfigurationExceptionDestroyFailedExceptionExecutionExceptionExpandVetoExceptionFontFormatExceptionGeneralSecurityExceptionGSSExceptionIllegalClassFormatExceptionInterruptedExceptionIntrospectionExceptionInvalidApplicationExceptionInvalidMidiDataExceptionInvalidPreferencesFormatExceptionInvalidTargetObjectTypeExceptionIOExceptionJAXBExceptionJMExceptionKeySelectorExceptionLambdaConversionExceptionLastOwnerExceptionLineUnavailableExceptionMarshalExceptionMidiUnavailableExceptionMimeTypeParseExceptionMimeTypeParseExceptionNamingExceptionNoninvertibleTransformExceptionNotBoundExceptionNotOwnerExceptionParseExceptionParserConfigurationExceptionPrinterExceptionPrintExceptionPrivilegedActionExceptionPropertyVetoExceptionReflectiveOperationExceptionRefreshFailedExceptionRemarshalExceptionRuntimeExceptionSAXExceptionScriptExceptionServerNotActiveExceptionSOAPExceptionSQLExceptionTimeoutExceptionTooManyListenersExceptionTransformerExceptionTransformExceptionUnmodifiableClassExceptionUnsupportedAudioFileExceptionUnsupportedCallbackExceptionUnsupportedFlavorExceptionUnsupportedLookAndFeelExceptionURIReferenceExceptionURISyntaxExceptionUserExceptionXAExceptionXMLParseExceptionXMLSignatureExceptionXMLStreamExceptionXPathException

public class Exception

extends Throwable

The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.

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


Exception 类和它的子类是 Throwable 的一种形式,用来表示一个合理的应用程序可能希望捕获的情形。

异常类和不是 RuntimeException 子类的任意子类 都是可检查异常。如果可检查异常可以在方法或构造方法执行过程中被抛出并且传播到方法和构造方法之外,那么这些异常就需要声明在方法或者构造方法的 throws 子句中。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值