处理 Exception 的几种实践,很优雅,被很多团队采纳!
阅读本文大概需要 5 分钟。
来自:http://ww7.rowkey.me/
在Finally块中清理资源或者使用try-with-resource语句
public void doNotCloseResourceInTry() {FileInputStream inputStream = null;try {File file = new File("./tmp.txt");inputStream = new FileInputStream(file);// use the inputStream to read a file// do NOT do thisinputStream.close();} catch (FileNotFoundException e) {log.error(e);} catch (IOException e) {log.error(e);}}
public void closeResourceInFinally() {FileInputStream inputStream = null;try {File file = new File("./tmp.txt");inputStream = new FileInputStream(file);// use the inputStream to read a file} catch (FileNotFoundException e) {log.error(e);} finally {if (inputStream != null) {try {inputStream.close();} catch (IOException e) {log.error(e);}}}}public void automaticallyCloseResource() {File file = new File("./tmp.txt");try (FileInputStream inputStream = new FileInputStream(file);) {// use the inputStream to read a file} catch (FileNotFoundException e) {log.error(e);} catch (IOException e) {log.error(e);}}
指定具体的异常
public void doNotDoThis() throws Exception {...}public void doThis() throws NumberFormatException {...}
对异常进行文档说明
/*** This method does something extremely useful ...** @param input* @throws MyBusinessException if ... happens*/public void doSomething(String input) throws MyBusinessException {...}
抛出异常的时候包含描述信息
try {new Long("xyz");} catch (NumberFormatException e) {log.error(e);}
首先捕获最具体的异常
public void catchMostSpecificExceptionFirst() {try {doSomething("A message");} catch (NumberFormatException e) {log.error(e);} catch (IllegalArgumentException e) {log.error(e)}}
# 不要捕获Throwable
public void doNotCatchThrowable() {try {// do something} catch (Throwable t) {// don't do this!}}
不要忽略异常
public void doNotIgnoreExceptions() {try {// do something} catch (NumberFormatException e) {// this will never happen}}
public void logAnException() {try {// do something} catch (NumberFormatException e) {log.error("This should never happen: " + e);}}
不要记录并抛出异常
try {new Long("xyz");} catch (NumberFormatException e) {log.error(e);throw e;}
17:44:28,945 ERROR TestExceptionHandling:65 - java.lang.NumberFormatException: For input string: "xyz"Exception in thread "main" java.lang.NumberFormatException: For input string: "xyz"at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)at java.lang.Long.parseLong(Long.java:589)at java.lang.Long.(Long.java:965)at com.stackify.example.TestExceptionHandling.logAndThrowException(TestExceptionHandling.java:63)at com.stackify.example.TestExceptionHandling.main(TestExceptionHandling.java:58)
如上所示,后面的日志也没有附加更有用的信息。如果想要提供更加有用的信息,
那么可以将异常包装为自定义异常。
public void wrapException(String input) throws MyBusinessException {try {// do something} catch (NumberFormatException e) {throw new MyBusinessException("A message that describes the error.", e);}}
包装异常时不要抛弃原始的异常
public void wrapException(String input) throws MyBusinessException {try {// do something} catch (NumberFormatException e) {throw new MyBusinessException("A message that describes the error.", e);}}
总结
点击上方小卡片,关注我的公众号
朕已阅 
评论

