0

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?

A Nice Guy
  • 2,676
  • 4
  • 30
  • 54
  • "the program terminates after executing the Logger in the catch block": You have two such catch blocks in your code snippets, which one are you referring to? Is the program terminating before the `CustomRestException` is thrown? – mbj Jan 16 '19 at 13:53
  • In what kind of application is the code being run? – prasad_ Jan 16 '19 at 13:57
  • 1
    somewhere the `CustomRestException` is being created (to enter that code) and almost sure it is being `throw`n - so perhaps this is causing the program to terminate.... and I would handle that Exception in my Exception - would kind of confusing if one Exception is thrown while creating another one (probably more relevant than the first one) - or better avoid that case at all: somehow I feel this is an indication that something else should have been checked before... – user85421 Jan 16 '19 at 14:02
  • 1
    BW: (custom) Exceptions are normally very simple, almost empty... – user85421 Jan 16 '19 at 14:08
  • I think there is no issue throwing and catching an exception (and handling it) within a custom exception. Program does not terminate after handling an exception in a catch-block (unless you have code to terminate the program in that block). – prasad_ Jan 16 '19 at 14:21
  • 1
    Putting together the error response really doesn't belong inside an exception like this. This exception is doing too much work. – Nathan Hughes Jan 16 '19 at 15:28
  • You need to provide more information because the behavior you describe is not explained by the code provided. – harogaston Jan 16 '19 at 18:25
  • @harogaston.Are you saying, with the given code, my expectation is correct? I have put up almost all the relevant code I can think of.Rest is mostly business logic. – A Nice Guy Jan 16 '19 at 18:33
  • When you say `the program terminates` you mean the application shuts down? – Taylor Jan 17 '19 at 03:28
  • Its a HTTP endpoint exposed by the JAVA API.The endpoint fails with internal error(status 500). And the only log entry is "[setUniformInterfaceExceptionParameters] Ecnountered error" Since the exception is handled, i think the log entry "(someMessage) CustomRestException " should also be there. – A Nice Guy Jan 17 '19 at 04:30

0 Answers0