I am using the below syntax to get the error code from ErrorCodeEnum class. How can I do this directly using an overloaded constructor in ErrorCodeEnum class?
new ServiceException(ErrorCodeEnum.ERROR_1005.getErrorCode() )
Create an overloaded constructor taking error code directly.
I have classes as below
public class MainException extends RuntimeException {
private String errorMessage;
private ErrorCodeEnum errorCodeEnum;
public MainException() {
}
public MainException (ErrorCodeEnum errorCodeEnum, String errorMessage) {
//super(errorMessage);
this.errorCodeEnum = errorCodeEnum;
this.errorMessage = errorMessage;
}
public String getErrorCode() {
return errorCode;
}
public String getErrorMessage() {
return errorMessage;
}
}
and base class as :
public class ServiceException extends MainException {
public ServiceException() {
}
public ServiceException(ErrorCodeEnum errorCodeEnum,String errorMessage) {
super(errorCodeEnum,errorMessage);
}
}
not able to overload it.
I’m guessing you want to ServiceException to accept an instance of ErrorCodeEnum directly?
What I’m guessing you have now:
To simply overload the constructor, you can do it like this:
You can then call
new ServiceException(ErrorCodeEnum.ERROR_1005)directly. As for the implementation of the new constructor, the easiest way would probably be to utilize the existing constructor and pass it the error code, which you can call with a reference tothis: