英文:
Exception handling with code and message in java
问题
这是我的异常处理类:
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;
}
}
在我的代码中,我像下面这样处理异常,但是我得到了 500 Internal Server Error:
}catch(IllegalArgumentException e) {
throw new MyException("301", e.getMessage());
}
以下是我的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());
}
}
任何帮助都会受到赞赏。提前感谢。
英文:
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
答案1
得分: 1
你可以通过以下方式创建自定义异常:
public class CustomException extends RuntimeException {
public CustomException(String errorMessage) {
super(errorMessage);
}
}
接下来,在你的方法中,你可以通过以下方式抛出这个异常:
}catch(IllegalArgumentException e) {
throw new CustomException(e.getMessage());
}
英文:
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());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论