I have a custom Java exception class, 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);
}
}
}
I am throwing this CustomRestException from a method:
public String myMethod() throws CustomRestException {
try{
//make some rest call here
}catch(UniformInterfaceException ue){
throw new CustomRestException (p,"POST",url,p.getReqBody(),ue);
}
}
then, I am catching this CustomRestException
somewhere else:
public Response myEndPointMethod(){
try{
//some code
myClassObj.myMethod(); //calling myMethod() above
//some code
} catch (CustomRestException e) {
LOGGER.error("(someMessage) CustomRestException ", e);
}
My problem
this.endPointErrorResponse = new JSONObject(ue.getResponse().getEntity(String.class));
If this line throws any exception (I have seen only JSONException
so far), the program terminates after executing this logger in the catch block:
LOGGER.info("[setUniformInterfaceExceptionParameters] Ecnountered error ",ex);
My Expectation
The logger LOGGER.error("(someMessage) CustomRestException ", e);
in myEndPointMethod()
should be called.
Are we not supposed to handle (try-catch) exceptions inside custom exceptions?
Any idea where I am going wrong?