來源:http://ww7.rowkey.me/
正文如下:
在Java中處理例外并不是一個簡單的事情,不僅僅初學者很難理解,即使一些有經驗的開發者也需要花費很多時間來思考如何處理例外,包括需要處理哪些例外,怎樣處理等等,
這也是絕大多數開發團隊都會制定一些規則來規范對例外的處理的原因,而團隊之間的這些規范往往是截然不同的,
本文給出幾個被很多團隊使用的例外處理最佳實踐,
在Finally塊中清理資源或者使用try-with-resource陳述句
當使用類似InputStream這種需要使用后關閉的資源時,一個常見的錯誤就是在try塊的最后關閉資源,
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 this
inputStream.close();
} catch (FileNotFoundException e) {
log.error(e);
} catch (IOException e) {
log.error(e);
}
}
上述代碼在沒有任何exception的時候運行是沒有問題的,但是當try塊中的陳述句拋出例外或者自己實作的代碼拋出例外,那么就不會執行最后的關閉陳述句,從而資源也無法釋放,
合理的做法則是將所有清理的代碼都放到finally塊中或者使用try-with-resource陳述句,
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 {
...
}
如上,NumberFormatException字面上即可以看出是數字格式化錯誤,
對例外進行檔案說明
當在方法上宣告拋出例外時,也需要進行檔案說明,和前面的一點一樣,都是為了給呼叫者提供盡可能多的資訊,從而可以更好地避免/處理例外,例外處理的 10 個最佳實踐,這篇也推薦看下,
在Javadoc中加入throws宣告,并且描述拋出例外的場景,
/**
* This method does something extremely useful ...
*
* @param input
* @throws MyBusinessException if ... happens
*/
public void doSomething(String input) throws MyBusinessException {
...
}
拋出例外的時候包含描述資訊
在拋出例外時,需要盡可能精確地描述問題和相關資訊,這樣無論是列印到日志中還是監控工具中,都能夠更容易被人閱讀,從而可以更好地定位具體錯誤資訊、錯誤的嚴重程度等,
但這里并不是說要對錯誤資訊長篇大論,因為本來Exception的類名就能夠反映錯誤的原因,因此只需要用一到兩句話描述即可,
try {
new Long("xyz");
} catch (NumberFormatException e) {
log.error(e);
}
NumberFormatException即告訴了這個例外是格式化錯誤,例外的額外資訊只需要提供這個錯誤字串即可,當例外的名稱不夠明顯的時候,則需要提供盡可能具體的錯誤資訊,
首先捕獲最具體的例外
現在很多IDE都能智能提示這個最佳實踐,當你試圖首先捕獲最籠統的例外時,會提示不能達到的代碼,當有多個catch塊中,按照捕獲順序只有第一個匹配到的catch塊才能執行,因此,如果先捕獲IllegalArgumentException,那么則無法運行到對NumberFormatException的捕獲,
public void catchMostSpecificExceptionFirst() {
try {
doSomething("A message");
} catch (NumberFormatException e) {
log.error(e);
} catch (IllegalArgumentException e) {
log.error(e)
}
}
不要捕獲Throwable
Throwable是所有例外和錯誤的父類,你可以在catch陳述句中捕獲,但是永遠不要這么做,如果catch了throwable,那么不僅僅會捕獲所有exception,還會捕獲error,而error是表明無法恢復的jvm錯誤,因此除非絕對肯定能夠處理或者被要求處理error,不要捕獲throwable,
public void doNotCatchThrowable() {
try {
// do something
} catch (Throwable t) {
// don't do this!
}
}
不要忽略例外
很多時候,開發者很有自信不會拋出例外,因此寫了一個catch塊,但是沒有做任何處理或者記錄日志,
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);
}
}
因此,僅僅當想要處理例外時才去捕獲,否則只需要在方法簽名中宣告讓呼叫者去處理
包裝例外時不要拋棄原始的例外
捕獲標準例外并包裝為自定義例外是一個很常見的做法,這樣可以添加更為具體的例外資訊并能夠做針對的例外處理,
需要注意的是,包裝例外時,一定要把原始的例外設定為cause(Exception有構造方法可以傳入cause),否則,丟失了原始的例外資訊會讓錯誤的分析變得困難,
public void wrapException(String input) throws MyBusinessException {
try {
// do something
} catch (NumberFormatException e) {
throw new MyBusinessException("A message that describes the error.", e);
}
}
總結
綜上可知,當拋出或者捕獲例外時,有很多不一樣的東西需要考慮,其中的許多點都是為了提升代碼的可閱讀性或者api的可用性,
例外不僅僅是一個錯誤控制機制,也是一個溝通媒介,因此與你的協作者討論這些最佳實踐并制定一些規范能夠讓每個人都理解相關的通用概念并且能夠按照同樣的方式使用它們,
總結了一些2020年的面試題,這份面試題的包含的模塊分為19個模塊,分別是: Java基礎、容器、多執行緒、反射、物件拷貝、JavaWeb例外、網路、設計模式、Spring/SpringMVC、SpringBoot/SpringCloud、Hibernate、MyBatis、RabbitMQ、Kafka、Zookeeper、MySQL、Redis、JVM,
獲取以下資料,關注公眾號:【有故事的程式員】,
記得點個關注+評論哦~
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/254330.html
標籤:其他
