sonarqube:(而是捕获特定异常子类型的列表)

ftf50wuq  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(516)

我有一个关于一般例外的问题。当您尝试执行多个操作时,我们如何知道要使用哪个非泛型异常。
例如:

  1. @PostConstruct
  2. protected void init() {
  3. try {
  4. HttpSession session = request.getSession();
  5. String policyInfo = (String) session.getAttribute("policyInfo");
  6. if(session.getAttribute("faxNumber") != null) {
  7. faxNumber = (String) session.getAttribute("faxNumber");
  8. }
  9. policyNumber = (String) session.getAttribute("policyNumber");
  10. JSONObject policyInfoObj = new JSONObject(policyInfo);
  11. JSONArray policiesArr = policyInfoObj.getJSONArray("policies");
  12. if (policiesArr.length() > 0) {
  13. JSONObject policyObj = policiesArr.getJSONObject(0);
  14. JSONArray insuredVehicle = policyObj.getJSONArray("insuredVehicle");
  15. checkInsuredVechile(insuredVehicle);
  16. termStartDate = policyObj.getString("effectiveDate");
  17. JSONArray addressArray = policyObj.getJSONArray("address");
  18. policySource = policyObj.getString("policySource");
  19. checkAddressArry(addressArray);
  20. }
  21. policyNumber = policyNumber.substring(0,5)+"-"+policyNumber.substring(5,7)+"-"+policyNumber.substring(7);
  22. response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
  23. }catch(Exception e) {
  24. logger.error("Exception in getting policy details",e);
  25. }
  26. }

所以对于 catch(Exception e) { 它将需要一个非通用的异常,但我很难确定它可以是什么。

omtl5h9j

omtl5h9j1#

您应该只捕获特定的例外,例如:

  1. catch(org.json.JsonException e)

而不是基类 Exception ,这意味着所有可能的已检查和未检查异常

相关问题