我有以下代碼。我們正在使用聲納 8.9 版本和 jdk 11。SonarQube 總是拋出一個關鍵問題“定義并拋出專用例外而不是使用通用例外”
try {
String stringPayload = jsonMapper.writeValueAsString(payload);
log.info("Feedzai request: {}"<some object>);
input.setPayload(new StringEntity(stringPayload, APPLICATION_JSON));
} catch (JsonProcessingException e) {
throw new RuntimeException(e.getMessage());
}
我試圖從以下位置替換 catch "RuntimeException":
throw new RuntimeException(e.getMessage());
扔新的
RuntimeException(String.format("RuntimeException during processing JSON %s", e.getMessage()),e);
但得到同樣的錯誤。你能請一些人幫助我嗎?
uj5u.com熱心網友回復:
RuntimeExteption 的定義:
RuntimeException 及其子類是未經檢查的例外。未經檢查的例外不需要在方法或建構式的
throws子句中宣告,如果它們可以通過方法或建構式的執行拋出并傳播到方法或建構式邊界之外。
您有兩個選擇:
- 創建自定義例外類
- 扔已經抓到
JsonProcessingException
第一個選項的代碼將是:
} catch (JsonProcessingException e) {
//log message somewhere
throw new MyCustomException(e.getMessage());
}
第二個選項的代碼將是:
} catch (JsonProcessingException e) {
//log message somewhere
throw;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/328259.html
