0

I want to write custom exception handling for my java code. I am using rest based service which returns a valid json response in case of success. In case of any exception, I want to return a proper json response with the error code and error message.

This is my Exception class

public class MyException extends Exception{
    
    private static final long serialVersionUID = -6823908633735492805L;

    protected String errorCode;

    protected String errorMessage;

    public MyException()
    {
        super();
    }

    public MyException(String errorCode)
    {
        super(errorCode, null);
        this.errorCode = errorCode;
    }

    public MyException(String errorMessage, String errorCode)
    {
        this.errorMessage = errorMessage;
        this.errorCode = errorCode;
    }

    public MyException(String message, Throwable cause)
    {
        super(message, cause);
    }

    public MyException(Throwable cause)
    {
        super(cause);
    }

    public String getErrorCode()
    {
        return errorCode;
    }

    public String getErrorMessage()
    {
        return errorMessage;
    }

    public MyException(Throwable cause, String errorCode, String errorMessage)
    {
        super(cause);
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
    }

}

In my code, i am handling the Exception like below but I am getting 500 Internal Server Error

}catch(IllegalArgumentException e) {
    throw new MyException("301",e.getMessage());            
}

Below is my rest api

@Path("{id}")
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Operation(operationId = "update")
@APIResponse(responseCode = "204")
public Response update(@Context SecurityContext context, String effort) throws MyException {
    try {
        service.update(effort);
        return Response.noContent().build();
    }catch(IllegalArgumentException e) {
        throw new MyException("301",e.getMessage());
    }
}

Any help is appreciated.. Thanks in advance

5
  • Are u creating REST API also , are u using Spring or Jersey ? Commented Jul 30, 2020 at 18:53
  • Please share your rest service. I doubt that the MyException class is the issue. Commented Jul 30, 2020 at 18:53
  • I m using helidon... Commented Jul 30, 2020 at 19:02
  • I have updated my rest api... Commented Jul 30, 2020 at 19:06
  • It's not enough to just inherit Exception. You have to inherit RuntimeException. Commented Jul 30, 2020 at 19:11

1 Answer 1

1

You can create your custom exception by

public class CustomException
      extends RuntimeException {
        public CustomException(String errorMessage) {
            super(errorMessage);
        }
    }

Next, In your method you can produce this exception by

}catch(IllegalArgumentException e) {
    throw new CustomException(e.getMessage());            
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.