Exceptions and exception handling are an important part in most applications. And modern programming languages support exceptions very well. Most of them provide try/catch/finally clauses for developers to handle exceptions. Here we talk about how to write try/catch/finally clauses in a more elegant way.

Here, we take I/O operations as our illustrated example, because I/O conerns three important steps when using: initialization, exception handing and cleanup.

Generally, some programmer might write like this:

// Declaration of objectsInputStream input = null;OutputStream output = null;try {     // Initialization input and output     input = new FileInputStream("readme.txt");     output = new FileOutputStream("ic_card.txt");          // Operations     int seg = 0;     byte[] buf = new byte[4096];     while ((seg = input.read(buf)) != -1) {          output.write(buf, 0, seg);     }} catch (FileNotFoundException e) {     // handling exception} catch (IOException e) {     // handling exception} finally {     // cleanup     try {          if (input != null) {             input.close();          }          if (output != null) {             output.close();          }      } catch (IOException e) {           // handling exception      }}
Such style has the following problems:
  1. There are extra try/catch inside outer try/catch to catch the same exception, which is might be redundant
  2. The variable input/output are declared beyond where they are needed, which have unnecessary life circle
  3. Two try/catch and try/catch in finally, which might be not elegant enough
The suggested way is like this:

try {     // Declarations     InputStream input = null;     OutputStream output = null;     try {           // Initialization input and output           input = new FileInputStream("readme.txt");           output = new FileOutputStream("ic_card.txt");           // Operations           int seg = 0;           byte[] buf = new byte[4096];           while ((seg = input.read(buf)) != -1) {               output.write(buf, 0, seg);           }      } finally {           // cleanup codes           if (input != null) {               input.close();           }           if (output != null) {               output.close();           }      }} catch (FileNotFoundException e) {     // handling exception} catch (IOException e) {     // handling exception}

This try/finally try/catch style is superior to previous version is because, it finishes all the tasks: declaration, initialization, operations and cleanup. All blocks are inside big try/catch so that all exceptions are get caught; Cleanup is inside try/finally so that we ensure to release taken resources. It also looks elegant.

Such try/finally try/catch clauses, however, have a fatal defect that exceptions could be ignored and lost. In try/finally, finally block gets executed even if when exceptions are thrown in try block. If finally block also throws exceptions, ones thrown in try blocks are ignored and lost. Outer catch clauses will never catch them. In all, this style might not catch all exceptions thrown in try blocks.

You can apply this try/finally try/catch style when:

  1. cleanup codes are not much and quite simple, like closing IO streams, shutdown connection.etc.
  2. cleanup codes are not likely to throw exceptions, or much less than initializations and operations.
  3. you do not care what exceptions are, but whether there are exceptions thrown
Besides these, you should use try/catch/finally, when specific exceptions are important in particular.
// Delcarationstry {     // Initializations          // Operations} catch {     // handling exceptions} finally {     // cleanup}

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. (android 实战总结)android对html支持接
  2. 仅使用CSS的输入字段的一部分
  3. 用于在表中强制换行的PHP或HTML/CSS解决
  4. html解析类库htmlparser.net使用方法
  5. 文本输入占位符不在IE和Firefox中显示
  6. 如何使用struts2访问表单数组字段
  7. HTML5实战与剖析之媒体元素(1、video标签
  8. DiggBar如何基于不在其域上的内容动态调
  9. Smarty快速入门之一
  10. css字体大小在苹果邮件中比gmail (iphone