捕获嵌套到另一个异常中的异常 [英] Catching an exception that is nested into another exception

查看:396
本文介绍了捕获嵌套到另一个异常中的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想捕获一个异常,它被嵌套到另一个异常中。
我现在这样做:

 } catch(RemoteAccessException e){
if( e!= null& e.getCause()!= null&& e.getCause()。getCause()!= null){
MyException etrp =(MyException)e.getCause()。的getCause();
...
} else {
throw new IllegalStateException(在调用服务'服务'时出错);
}
}

有没有办法做到这一点更有效率和优雅?

解决方案

没有更优雅的方式有选择地捕捉嵌套异常。我想如果你这样的嵌套异常捕获很多,你可以将代码重构成一个通用的实用方法。但是它仍然不会优雅或高效。



优雅的解决方案是消除异常嵌套。或者不首先链接异常,或者(有选择地)解开并将堆栈中的嵌套异常进一步重新抛出。



异常往往被嵌套为3个原因:


  1. 您已经决定原始异常的细节不太可能对应用程序的错误恢复很有用...但是你想保存它们用于诊断目的。


  2. 你正在实现不允许检查异常的API方法,但你的代码(不可避免地)会抛出这样的异常。 (一个常见的解决办法是在一个未经检查的异常中走私异常。)


  3. 你正在懒惰,转向不同的一组不相关的例外,以避免在方法签名 1 中有很多检查异常。


在第一种情况下,如果您需要区分包装的例外情况,您的初始假设/设计是不正确的,理想的解决方案是更改方法签名,以便可以摆脱嵌套。



在第二种情况下,一旦控件已经通过有问题的API方法,您可能应该解开异常。



在第三个你应该重新考虑你的异常处理策略;这样做正确。






1 - 事实上,这样做的半合法原因之一已经消失由于在Java 7中引入了多异常捕获语法。


I want to catch an exception, that is nested into another exception. I'm doing it currently this way:

} catch (RemoteAccessException e) {
            if (e != null && e.getCause() != null && e.getCause().getCause() != null) {
            MyException etrp = (MyException) e.getCause().getCause();
            ...
            } else {
            throw new IllegalStateException("Error at calling service 'service'");
            }
        }

Is there a way to do this more efficient and elegant?

解决方案

There is no more elegant way of selectively "catching" nested exceptions. I suppose if you did this kind of nested exception catching a lot, you could possibly refactor the code into a common utility method. But it still won't be either elegant or efficient.

The elegant solution is to do away with the exception nesting. Either don't chain the exceptions in the first place, or (selectively) unwrap and rethrow the nested exceptions further up the stack.

Exceptions tend to be nested for 3 reasons:

  1. You have decided that the details of the original exception are unlikely to be useful for the application's error recovery ... but you want to preserve them for diagnostic purposes.

  2. You are implementing API methods that don't allow checked exceptions but your code (unavoidably) throws such exceptions. (A common workaround is to "smuggle" the exception inside an unchecked exception.)

  3. You are being lazy and turning a diverse set of unrelated exceptions into a single exception to avoid having lots of checked exceptions in your method signature1.

In the first case, if you need to discriminate on the wrapped exceptions your initial assumptions / design is incorrect, and the ideal solution is change method signatures so that you can get rid of the nesting.

In the second case, you probably should unwrap the exceptions as soon as control has passed the problematic API method.

In the third case, you should rethink your exception handling strategy; i.e. do it properly.


1 - Indeed, one of the semi-legitimate reasons for doing this has gone away due to the introduction of the multi-exception catch syntax in Java 7.

这篇关于捕获嵌套到另一个异常中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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