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

2024-05-09Java开发问题
0

本文介绍了SonarQube:(改为获取特定异常子类型的列表)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

I have a question about generic exceptions. How would we know which non-generic exception to use when you have a try that does multiple things.

例如:

  @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) { 它将需要一个非泛型异常,但我无法确定它可能是什么.

So for catch(Exception e) { it will need a non-generic exception, but I am having trouble to determine what it can be.

推荐答案

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

You should catch only specific exeption like:

catch(org.json.JsonException e)

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

and not the base class Exception, which means all possible checked and unchecked Exceptions

这篇关于SonarQube:(改为获取特定异常子类型的列表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13