我有一个关于泛型异常的问题。当你尝试做多件事的时候,我们怎么知道应该使用哪个非泛型异常呢?
例如:
@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) {,它将需要一个非泛型异常,但我在确定它是什么时遇到了麻烦。
发布于 2020-11-24 02:50:43
您应该只捕获特定的exeption,如:
catch(org.json.JsonException e)而不是基类Exception,这意味着所有可能的已检查和未检查的异常
https://stackoverflow.com/questions/64974465
复制相似问题