[ZT]深入java.lang.Throwable

本文详细解释了Java中Throwable类的cause机制,包括其应用场景和设计目的。主要探讨了两个场景:多层抽象结构中异常的传递和符合通用接口但需要处理底层异常的情况。
jdk中关于java.lang.Throwable的描述是这样的:
            A throwable contains a snapshot of the execution stack of its thread at the time it was created. It can also contain a message string that gives more information about the error. Finally, it can contain a <i>cause</i>:another throwable that caused this throwable to get thrown.  The cause facility is new in release 1.4.  It is also known as the <i>chained exception</i> facility, as the cause can, itself, have a cause, and so on, leading to a "chain" of exceptions, each caused by another.
           One reason that a throwable may have a cause is that the class that throws it is built atop a lower layered abstraction, and an operation on the upper layer fails due to a failure in the lower layer.  It would be bad design to let the throwable thrown by the lower layer propagate outward, as it is generally unrelated to the abstraction provided by the upper layer.Further, doing so would tie the API of the upper layer to the details of its implementation, assuming the lower layer's exception was a checked exception.  Throwing a "wrapped exception" (i.e., an exception containing a cause) allows the upper layer to communicate the details of the failure to its caller without incurring either of these shortcomings.  It preserves the flexibility to change the implementation of the upper layer without changing its API (in particular, the set of exceptions thrown by its methods).
           A second reason that a throwable may have a cause is that the method that throws it must conform to a general-purpose interface that does not permit the method to throw the cause directly.  For example, suppose a persistent collection conforms to the {@link java.util.Collection} interface, and that its persistence is implemented atop <tt>java.io</tt>.  Suppose the internals of the <tt>put</tt> method can throw an {@link java.io.IOException IOException}.  The implementation can communicate the details of the <tt>IOException</tt> to its caller while conforming to the <tt>Collection</tt> interface by wrapping the <tt>IOException</tt> in an appropriate unchecked exception.  (The specification for the persistent collection should indicate that it is capable of throwing such exceptions.)

           为什么Throwable的需要包含一个cause?原因有两个:
           1)在一个多层的结构当中,底层的操作可能抛出了一些异常,假设这些异常都是checked exception, 为了不让这些底层的异常的信息丢失,必须catch住,或者在上层的方法中throws出来(异常传递),异常传递的做法很多时候是有害的,因为这样做相当于更改了自己的api, 上层的api受到底层的api的约束的做法是不明智的。 所以好的做法就是catch底层的exception, 然后将它转化为上层的exception,转化的方法就是通过Throwable的cause来转化。
            2)如果有一个interface, 定义了一个方法doMethod(), 但是这个方法不扔出任何的exception; 有一个具体类implements了这个interface的doMethod(), 实现的过程中遇到了底层的checked exception, 所以必须catch, 但是由于doMethod()不扔出任何的异常, 所以只能够将checked exception封装在一个RuntimeException里边, 就像
      public void doMethod()
      {
            try
            {
                   ...
            }
            catch(CheckedException ex)
            {
                   throw new RuntimeException(ex);
            }
      }
      这么做的结果是不会影响interface的api, 而且还保留了CheckedException的信息。 
下载前必看:https://pan.quark.cn/s/a4b39357ea24 在本资料中,将阐述如何运用JavaScript达成单击下拉列表框选定选项后即时转向对应页面的功能。 此种技术适用于网页布局中用户需迅速选取并转向不同页面的情形,诸如网站导航栏或内容目录等场景。 达成此功能,能够显著改善用户交互体验,精简用户的操作流程。 我们须熟悉HTML里的`<select>`组件,该组件用于构建一个选择列表。 用户可从中选定一项,并可引发一个事件来响应用户的这一选择动作。 在本次实例中,我们借助`onchange`事件监听器来实现当用户在下拉列表框中选定某个选项时,页面能自动转向该选项关联的链接地址。 JavaScript里的`window.location`属性旨在获取或设定浏览器当前载入页面的网址,通过变更该属性的值,能够实现页面的转向。 在本次实例的实现方案里,运用了`eval()`函数来动态执行字符串表达式,这在现代的JavaScript开发实践中通常不被推荐使用,因为它可能诱发安全问题及难以排错的错误。 然而,为了本例的简化展示,我们暂时搁置这一问题,因为在更复杂的实际应用中,可选用其他方法,例如ES6中的模板字符串或其他函数来安全地构建和执行字符串。 具体到本例的代码实现,`MM_jumpMenu`函数负责处理转向逻辑。 它接收三个参数:`targ`、`selObj`和`restore`。 其中`targ`代表要转向的页面,`selObj`是触发事件的下拉列表框对象,`restore`是标志位,用以指示是否需在转向后将下拉列表框的选项恢复至默认的提示项。 函数的实现通过获取`selObj`中当前选定的`selectedIndex`对应的`value`属性值,并将其赋予`...
### JSP中exception隐式对象的类类型及原因分析 在JSP中,`exception`是一个隐式对象,用于处理页面中的异常情况。它的类型是`java.lang.Throwable`,而不是`java.lang.Exception`[^3]。以下是详细的原因分析: #### 1. `exception`对象的定义与作用 `exception`对象是在JSP页面中专门用于捕获和处理异常的对象。它只能在指定了`<%@ page errorPage="..." %>`或`<%@ page isErrorPage="true" %>`的页面中使用[^4]。该对象继承自`java.lang.Throwable`,这意味着它可以表示所有可能的异常类型,包括`Exception`及其子类,以及`Error`及其子类。 #### 2. `java.lang.Throwable`与`java.lang.Exception`的区别 `java.lang.Throwable`是Java中所有异常和错误的基类。它有两个直接子类:`Exception`和`Error`。 - `Exception`表示程序运行时可以被捕获和处理的异常,例如`NullPointerException`、`ArrayIndexOutOfBoundsException`等。 - `Error`表示严重的系统级问题,通常无法被程序捕获或处理,例如`OutOfMemoryError`、`StackOverflowError`等。 由于`exception`对象需要能够表示所有可能的异常情况,因此其类型被定义为`java.lang.Throwable`,以确保能够涵盖`Exception`和`Error`的所有子类[^5]。 #### 3. 为什么不是`java.lang.Exception` 如果将`exception`对象的类型定义为`java.lang.Exception`,那么它将无法表示`Error`及其子类。然而,在实际开发中,某些系统级错误(如内存不足)也可能需要被捕获和处理。为了确保`exception`对象能够处理所有可能的异常情况,包括程序性异常和系统级错误,因此选择了更广泛的基类`java.lang.Throwable`作为其类型[^3]。 #### 示例代码 以下是一个简单的JSP页面示例,展示如何使用`exception`对象: ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isErrorPage="true"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Error Page</title> </head> <body> <h1>An error occurred!</h1> <p>Exception message: <%= exception.getMessage() %></p> <p>Exception class: <%= exception.getClass().getName() %></p> </body> </html> ``` 在上述代码中,`exception`对象被用来获取异常消息和异常类的名称[^4]。 --- ### 总结 `exception`隐式对象的类型是`java.lang.Throwable`,而不是`java.lang.Exception`,这是因为它需要能够表示所有可能的异常情况,包括`Exception`及其子类,以及`Error`及其子类。这种设计确保了JSP页面能够在各种异常场景下正常工作[^3]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值