Catch a list of specific exception subtypes instead for unchecked exceptions

218 views Asked by At

How do I determine what exception to use for unchecked exceptions? For example

    @Override
    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) {
        LOG.info("Inisde FarmersLegalAuditJsonDataServlet do post method");
        try {
            String auditKey = request.getParameter("auditKey") != null ? request.getParameter("auditKey") : StringUtils.EMPTY;
            LOG.info("auditKey:{}",auditKey);
            
            if (StringUtils.isNotEmpty(auditKey)) {     
                //Calling service to serach in the repository
                String redirectPath = farmersLegalAuditSearchService.getJsonFilePath(auditKey, request);
                LOG.info("redirectPath:{}",redirectPath);
                
                if(StringUtils.isNotEmpty(redirectPath) && !StringUtils.equalsIgnoreCase(redirectPath, "EMPTY_RESULT")) {
                    response.sendRedirect(redirectPath);
                } else if(StringUtils.isNotEmpty(redirectPath) && StringUtils.equalsIgnoreCase(redirectPath, "EMPTY_RESULT")) {
                    response.getWriter().print("No results found");
                } else {
                    response.getWriter().print("There is some backend issue. Please try later.");
                }
            } else {
                LOG.info("auditKey is empty/null");
                response.getWriter().print("There is some backend issue. Please try later.");
            }
        } catch (IOException ioe) {
            LOG.error("IOException:", ioe);
        } catch(Exception ex) {
            LOG.error("Exception:",ex);
        }
    }

For catch(Exception ex) {LOG.error("Exception:",ex); } SonarQube does not want the generic exception, note when I delete the catch(Exception E), the project builds just fine, does it even need the exception, and if it does which one should I use?

0

There are 0 answers