java exception 包_什么是Java中的异常包装?

Java中的异常包装是捕获一个异常,将其包装到另一个异常中并重新抛出。这有助于隐藏底层实现细节,提供更具体的错误信息。异常传播是指异常从方法逐层向上抛出,直到被捕获或到达调用栈底。包装异常允许转换异常类型,如将已检查异常转换为运行时异常,或提供更相关的异常信息。在实际应用中,包装异常可以简化客户端代码,使错误处理更集中且具有针对性。
摘要由CSDN通过智能技术生成

Java中的异常包装是什么? 在异常处理中有什么用? 它与异常传播有何不同?

Exception wrapping is when you catch an exception, wrap it

in a different exception and throw that exception.

Here is an example:

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/exception-propagation

您知道哪种情况在哪种情况下有用吗? 什么是" MyException"。 它是一个自定义异常还是它也可以是Java检查的异常?

@BoratSagdiyev感谢您的投票,我不知道您在此答案中发现了什么错误,迫使您单击该按钮。 如果您有任何疑问,您可以要求评论,而不是挑战我所知道的和我所不知道的.........而且我看到您在这里评论的每个答案都在这里被复制。 .... OP来到这里是因为他无法在其他任何地方找到正确的答案。 如果有人提供答案,那没有人受伤……

我认为Neeraj的答案是正确的。作为补充,我认为一个特别好的案例是通过抽象异常来管理抛出的异常数量。为了扩展Neeraj的示例:

class Manager {

public void sendPerson() throws ManagerException {

try{

Person person = dao.readPerson();

Socket socket = getSocket();

OutputStream os = socket.getOutputStream();

String personJson = objectMapper.writeValueAs(person);

os.write(personJson);

} catch (SQLException | SocketException | OutputStreamException | SerializationException e) {

throw new ManagerException("error text", e);

}

}

}

这样,客户端只需要执行以下操作:

try {

manager.sendPerson();

} catch (ManagerException e) {

// Handle fail by manager

}

而不用担心管理器中可能出问题的详细信息。

一个用例是将已检查的异常转换为运行时异常,反之亦然。

或可能只是一个命名问题。假设您在代码中的某个时刻捕获了SQLException,但是您可以推断出这是因为用户未登录。然后您可以捕获它并抛出自己的自定义NotLoggedInException。

这个答案是从这里获得的:http://www.javapractices.com/topic/TopicAction.do?Id=77

Data can be stored in various ways, for example:

a relational database

text files

on the web (for example, fetching the weather forecast from a web site)

If the storage method changes, then the low level Exception objects thrown by the data access layer can also change. For example, when the data store is moved from text files to a relational database, IOException is replaced with SQLException.

In order to prevent such a change from propagating to higher layers, one can wrap such low level Exceptions in a generic"data exception" wrapper, designed exclusively to protect the higher layers from such changes.

现在我们将看到一个示例...

public class ResourceLoader {

public loadResource(String resourceName) throws ResourceLoadException {

Resource r;

try {

r = loadResourceFromDB(resourceName);

}

catch (SQLException e) {

throw new ResourceLoadException("SQL Exception loading resource"

+ resourceName:" + e.toString());

}

}

}

loadResource的实现合理地使用了异常。通过抛出ResourceLoadException而不是SQLException(或实现引发的任何其他异常),loadResource对调用者隐藏了实现,从而使更改实现变得更容易,而无需修改调用代码。此外,loadResource()引发的异常-ResourceLoadException-与它执行的任务直接相关:加载资源。低级异常SQLException和IOException与该方法执行的任务没有直接关系,因此可能对调用方没有太大帮助。此外,此包装保留了原始异常的错误消息,因此用户知道为什么无法加载资源(可能是由于连接错误或不正确的用户名或密码),并可以采取纠正措施。

这个答案来自这里-javapractices.com/topic/TopicAction.do?Id=77

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值