Exception translation: higher layers should catch lower-level exceptions and, in their place, throw exceptions that can be explained in terms of the higher-level abstraction.

// Exception Translation

try {

// Use lower-level abstraction to do our bidding

...

} catch(LowerLevelException e) {

throw new HigherLevelException(...);

}

Example

/**

* Returns the element at the specified position in this list.

* @throws IndexOutOfBoundsException if the index is out of range

* ({@code index < 0 || index >= size()}).

*/

public E get(int index) {

ListIterator<E> i = listIterator(index);

try {

return i.next();

} catch(NoSuchElementException e) {

throw new IndexOutOfBoundsException("Index: " + index);

}

}

ExceptionChain: The lower-level exception (the cause) is passed to the higher-level exception, which provides an accessor method (Throwable.getCause ) to retrieve the lower-level exception:

// Exception Chaining

try {

... // Use lower-level abstraction to do our bidding

} catch (LowerLevelException cause) {

throw new HigherLevelException( cause);

}

// Exception with chaining-aware constructor

class HigherLevelException extends Exception {

HigherLevelException(Throwable cause) {

super(cause);

}

}

Note

Most standard exceptions have chaining-aware constructors. For exceptions that don't, you can set the cause using Throwable's initCause method. Not only does exception chaining let you access the cause programmatically (with getCause ), but it integrates the cause's stack trace into that of the higher-level exception.

Principle

While exception translation is superior to mindless propagation of exceptions from lower layers, it should not be overused.

Solutions:

  1. The best way to deal with exceptions from lower layers is to avoid them by checking the validity of the higher-level method's parameters before passing them on to lower layers.
  2. The next best thing is to have the higher layer silently work around these exceptions, insulating the caller of the higher-level method from lower-level problems.(Log the exception using some appropriate logging facility such as java.util.logging which allows an administrator to investigate the problem, while insulating the client code and the end user from it.

Summary

If it isn't feasible to prevent or to handle exceptions from lower layers, use exception translation, unless the lower-level method happens to guarantee that all of its exceptions are appropriate to the higher level. Chaining provides the best of both worlds: it allows you to throw an appropriate higher-level exception, while capturing the underlying cause for failure analysis (Item 63).

最新文章

  1. 谢欣伦 - OpenDev原创教程 - 蓝牙设备查找类CxBthRadio &amp; CxBthRadioFind
  2. Ubuntu使用MyEclipse闪退的解决办法
  3. oracle的sqlnet.ora,tnsnames.ora,listener.ora三个配置文件
  4. C# 获取当前操作系统是32位还是64位
  5. Metrics-Java版的指标度量工具之二
  6. 【C语言】C语言运算符
  7. JS原型对象通俗&quot;唱法&quot;
  8. [转载]ExtJs4 笔记(7) Ext.tip.ToolTip 提示
  9. 一个简单例子:贫血模型or领域模型
  10. 微信分享JS接口失效说明及解决方案
  11. iOS常见问题(4)
  12. fork、vfork、clone区别
  13. Vim Vundle 插件管理器
  14. 更新项目经常使用的Linux命令
  15. Linux系统CentOS6.2版本号下安装JDK7具体过程
  16. 使用express创建新应用的骨架
  17. python_如何获取文件状态
  18. 《CLR Via C#》学习--线程开销
  19. 在Python中用Request库模拟登录(三):Discuz论坛(未加密,有验证码,有隐藏验证)
  20. Spring Cloud Commons模块

热门文章

  1. java io系列15之 DataOutputStream(数据输出流)的认知、源码和示例
  2. js实现页面a向页面b传参的方法
  3. Struts2 之 实现文件上传和下载
  4. struts2基础——标签
  5. 0527Sprint总结,读书笔记与提问
  6. ASP.NEt MVC5--创建下拉列表
  7. 2015百度之星 IP聚合
  8. Brute Force &amp; STL --- UVA 146 ID Codes
  9. sencha gridpanel改变单元格颜色
  10. SVN 忽略文件但不删除文件