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모든 가능한 확인 및 확인되지 않은 예외를 의미합니다.