java 异常处理最佳实践,Java异常处理的最佳实践

I wrote the following code recently; it uses a lot of exception handling. I think it makes the code look very unreadable. I could shorten the code by catching generic exception, such as

catch (Exception e){

e.printStackTrace();

}

but I also heard that catching generic exception is not a good coding practice.

public class DataAnalyzerTester {

/**

* @param args args[0] stores the filename

* @exception NoSuchElementException if user attempts to access empty list element

* @exception ArithmeticException if user attempts to divide by 0

* @exception ArrayIndexOutOfBoundsException if user supplied less than 3 arguments

* @exception IOException problems with creating and writing files

* @exception RuntimeException if user attempts to pass empty list to constructor

*/

public static void main(String[] args) {

try{

//some code

} catch (NoSuchElementException e) {

System.out.println("Accessing element that does not exist: " + e.toString());

} catch (ArithmeticException e) {

System.out.println("Division by zero: " + e.toString());

} catch (ArrayIndexOutOfBoundsException e) {

System.out.println("Please supply a command line arguement that specifies your file path: " + e.toString());

} catch (IOException e) {

System.out.println("Other IO errors: " + e.toString());

} catch (RuntimeException e) {

System.out.println(e.toString());

}

}

}

I would like to know if there is any better and cleaner way to catch multiple exceptions.

解决方案

First, unless you have very good reason, never catch RuntimeException, Exception or Throwable. These will catch most anything that is thrown, and Throwable will catch everything, even those things you're not meant to catch, like OutOfMemoryError.

Second, avoid catching runtime exceptions unless it directly impedes with the critical operation of your program. (But seriously, if anyone sees you catch a NullPointerException, they are within their full rights to call you out on it.) The only exceptions you should be bothering with are those that you are required to handle. Out of your list of exceptions, the only one you should bother with is IOException. The rest are the result of not enough tests, or sloppy coding; those shouldn't occur in the normal run time of your application.

Third, in Java 7, you have the ability to do a multi-catch statement for your exceptions, if the exceptions are mutually exclusive. The example linked does a good job of explaining it, but if you were to encounter code that threw both an IOException and an SQLException, you could handle it like this:

try {

// Dodgy database code here

catch (IOException|SQLException ex) {

logger.log(ex);

throw ex;

}

This cleans things up a bit, as you don't have unwieldy and huge chains of exceptions to catch.

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值