什么是Java中的异常包装? [英] What is exception wrapping in Java?

查看:175
本文介绍了什么是Java中的异常包装?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java中的异常包装是什么?在异常处理中如何有用?如何与异常传播有所区别?

What is Exception wrapping in Java? How is it useful in exception handling? How it differs from exception propagation?

推荐答案


异常包装当你捕获一个异常时,将
包装在一个不同的异常中并抛出该异常。

Exception wrapping is when you catch an exception, wrap it in a different exception and throw that exception.

这是一个例子:

 try{
       dao.readPerson();
 } catch (SQLException sqlException) {
       throw new MyException("error text", sqlException);
 }


资料来源: http://tutorials.jenkov.com/java-exception-handling/exception-wrapping.html

另一方面


异常传播:从
的顶部首先抛出一个异常,如果没有被捕获,它会将调用堆栈下拉到
以前的方法,如果没有捕获在那里,异常再次下降到
以前的方法,依此类推,直到它们被捕获或直到它们
到达调用堆栈的最底层。

Exception Propagation: An exception is first thrown from the top of the stack and if it is not caught, it drops down the call stack to the previous method, if not caught there, the exception again drops down to the previous method, and so on until they are caught or until they reach the very bottom of the call stack.

资料来源: http://www.javatpoint.com/异常传播

这篇关于什么是Java中的异常包装?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆