exceptions - Basic Exceptions

本文深入探讨了Java中异常处理的基本原理,包括异常条件、异常对象的创建与抛出过程,以及异常处理机制如何寻找合适的处理器继续执行程序。同时,区分了运行时异常和检查异常,并介绍了Throwable、Error及Exception类在异常处理中的角色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

An exceptional condition is a problem that prevents the continuation of the current method or scope. It’s important to distinguish an exceptional condition from a normal problem, when you have enough information in the current context to somehow cope with the difficulty.

When we throw an exception, two things happen.

  1. The exception object is created in the same way as any Java object: on the heap, with new.
  2. The current path of execution (the one you can't continue) is stopped and the reference for the exception object is ejected from the current context. Now the exception-handling mechanism takes over and begins to look for an appropriate place(the exception handler) to continue executing the program.
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 eitherRuntimeExceptionorErrorare regarded as checked exceptions.

Instances of two subclasses, Error and Exception, are conventionally used to indicate that exceptional situations have occurred. Typically, these instances are freshly created in the context of the exceptional situation so as to include relevant information (such as stack trace data).

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

There are two basic models in exception-handling theory: termination (Java support this) and resumption.

references:

1. On Java 8 - Bruce Eckel

2. https://docs.oracle.com/javase/8/docs/api/java/lang/Throwable.html

3. http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/lang/Exception.java 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值