繁体   English   中英

SonarQube:(改为捕获特定异常子类型的列表)

[英]SonarQube: (Catch a list of specific exception subtypes instead)

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

例如:

  @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) {它将需要一个非通用异常,但我无法确定它是什么。

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

catch(org.json.JsonException e)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM