Java-自定义异常内带有try-catch异常的问题

时间:2019-01-16 13:39:22

标签: java exception-handling custom-exceptions

我有一个自定义Java异常类CustomRestException

 import org.codehaus.jettison.json.JSONObject;

 public class CustomRestException extends Exception{

    private String httpStatusMessage;    
    private Integer httpStatusCode;

    private String endpointBody;
    private String callType;        
    private List<HashMap<String,String>> headerParams;
    private List<HashMap<String,String>> queryParams;

    private JSONObject endPointErrorResponse;   

    public CustomRestException (RestCallParameters p,String type,String url,String body,UniformInterfaceException ue) {

       super(p.getCollectionErrMsg());      
       this.endpointBody= body;
       this.endPointUrl = url;
       this.callType = type;
       setUniformInterfaceExceptionParameters(ue);
    }

    private void setUniformInterfaceExceptionParameters(UniformInterfaceException ue) {
        try {
            this.httpStatusMessage = ue.getMessage();    
            this.httpStatusMessage  = ue.getResponse().getClientResponseStatus().toString();
            this.httpStatusCode = ue.getResponse().getStatus();

            this.endPointErrorResponse =
                    new JSONObject(ue.getResponse().getEntity(String.class));            
        }
        catch (Exception ex) {
             LOGGER.info("[setUniformInterfaceExceptionParameters]  Ecnountered error ",ex);
        }
    }
}

我正在从方法中抛出此CustomRestException:

public String myMethod() throws CustomRestException {
   try{
       //make some rest call here
   }catch(UniformInterfaceException ue){
       throw new CustomRestException (p,"POST",url,p.getReqBody(),ue);   
   }
}

然后,我在其他地方抓到了CustomRestException

public Response myEndPointMethod(){
    try{
        //some code
        myClassObj.myMethod();     //calling myMethod() above
        //some code

    } catch (CustomRestException e) {             
       LOGGER.error("(someMessage) CustomRestException ", e);       
    }




我的问题

this.endPointErrorResponse = new JSONObject(ue.getResponse().getEntity(String.class));

如果此行引发任何异常(到目前为止,我仅见过JSONException),则程序在catch块中执行此logger后终止程序:

LOGGER.info("[setUniformInterfaceExceptionParameters]  Ecnountered error ",ex);



我的期望

 应该调用LOGGER.error("(someMessage) CustomRestException ", e);中的记录器myEndPointMethod()



我们不应该在自定义异常中处理(try-catch)异常吗?
知道我要去哪里错了吗?

0 个答案:

没有答案