在Java中处理异常,包括代码和消息。

huangapple go评论75阅读模式
英文:

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());            
}

huangapple
  • 本文由 发表于 2020年7月31日 02:45:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63179478.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定