SonarQube :(代わりに特定の例外サブタイプのリストをキャッチしてください)
Nov 23 2020
一般的な例外について質問があります。複数のことを実行しようとしたときに、どの非ジェネリック例外を使用するかをどのように知ることができますか。
例えば:
@PostConstruct
protected void init() {
try {
HttpSession session = request.getSession();
String policyInfo = (String) session.getAttribute("policyInfo");
if(session.getAttribute("faxNumber") != null) {
faxNumber = (String) session.getAttribute("faxNumber");
}
policyNumber = (String) session.getAttribute("policyNumber");
JSONObject policyInfoObj = new JSONObject(policyInfo);
JSONArray policiesArr = policyInfoObj.getJSONArray("policies");
if (policiesArr.length() > 0) {
JSONObject policyObj = policiesArr.getJSONObject(0);
JSONArray insuredVehicle = policyObj.getJSONArray("insuredVehicle");
checkInsuredVechile(insuredVehicle);
termStartDate = policyObj.getString("effectiveDate");
JSONArray addressArray = policyObj.getJSONArray("address");
policySource = policyObj.getString("policySource");
checkAddressArry(addressArray);
}
policyNumber = policyNumber.substring(0,5)+"-"+policyNumber.substring(5,7)+"-"+policyNumber.substring(7);
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
}catch(Exception e) {
logger.error("Exception in getting policy details",e);
}
}
したがって、catch(Exception e) {
一般的でない例外が必要になりますが、それが何であるかを判断するのに苦労しています。
回答
Jens Nov 23 2020 at 18:50
次のような特定の例外のみをキャッチする必要があります。
catch(org.json.JsonException e)
基本クラスException
ではありません。これは、チェックされている例外とチェックされていない例外のすべてを意味します。