Java 使用生成器模式创建异常?
我的java代码中有一个异常,它有几个属性可以传递到构造函数中。因此,我认为使用生成器模式创建异常会很好,因此我创建了如下异常:Java 使用生成器模式创建异常?,java,exception-handling,builder,Java,Exception Handling,Builder,我的java代码中有一个异常,它有几个属性可以传递到构造函数中。因此,我认为使用生成器模式创建异常会很好,因此我创建了如下异常: public class ApplicationException extends Exception { private static final long serialVersionUID = -8999932578270387947L; /** * contains redundantly the HTTP status of t
public class ApplicationException extends Exception {
private static final long serialVersionUID = -8999932578270387947L;
/**
* contains redundantly the HTTP status of the response sent back to the client in case of error, so that
* the developer does not have to look into the response headers. If null a default
*/
Integer status;
/** application specific error code */
int code;
/** link documenting the exception */
String link;
/** detailed error description for developers*/
String developerMessage;
/**
*
* @param status
* @param code
* @param message
* @param developerMessage
* @param link
*/
protected ApplicationException(String message) {
super(message);
}
...
public static Builder getBuilder(String message) {
return new Builder(message);
}
public static class Builder {
private Integer status;
private int code;
private String link;
private String message;
private String developerMessage;
public Builder(String message) {
this.message = message;
}
public Builder status(int status) {
this.status = status;
return this;
}
public Builder code(int code) {
this.code = code;
return this;
}
public Builder developerMessage(String developerMessage) {
this.developerMessage = developerMessage;
return this;
}
public Builder link(String link) {
this.link = link;
return this;
}
public ApplicationException build() {
if (StringUtils.isBlank(message)) {
throw new IllegalStateException("message cannot be null or empty");
}
if (StringUtils.isBlank(link)){
link = AppConstants.API_SUPPORT_EMAIL;
}
ApplicationException created = new ApplicationException(message);
created.status = status;
created.code = code;
created.developerMessage = developerMessage;
created.link = link;
return created;
}
}
}
现在,我可以创建如下异常:
throw ApplicationException.getBuilder("This is the general error message")
.status(404)
.code(StatusCode.RESOURCE_DOES_NOT_EXIST)
.developerMessage("The resource does not exist")
.build();
我目前面临的问题是,我希望创建与此基本异常不同的异常。例如,ValidationException应扩展自ApplicationException
但是build()方法已经返回了具体类型ApplicationException。目前我被卡住了,因为我不太熟悉泛型的使用,甚至不知道它是否可以在异常类中使用。您可以传递
类您正在处理多少不同的异常子类型?如果不是太多,您可以在生成器中为每个异常类型创建一个build()
方法:
throw ApplicationException.getBuilder("This is the general error message")
.status(404)
.code(StatusCode.RESOURCE_DOES_NOT_EXIST)
.developerMessage("The resource does not exist")
.buildValidation(); // or buildSomeOther()
它不是泛型的,但非常简单,可以很好地与IDE autocomplete配合使用您需要编译时类型为ValidationException
,还是只关心执行时类型?我认为在执行期间,如果它使您的思考更容易,就足够了。我只是不想只处理一种常规异常类型。我想指出的是,在验证一个对象的过程中,某个对象出现了问题。请使用更多。请看一下Spring的代码,在您给出的代码中是否有任何东西可以“告诉”构建器创建一个验证异常,而不是一般的应用程序异常?(顺便说一句,你的例外字段应该是私有的和最终的,我的意思是。)@BoristheSpider thx我有一个调查@JonSkeet不,我没有任何指示,谢谢你的这种方法。现在我明白了乔恩的问题。我需要编译时类型。否则,所有的类都只表明它们抛出ApplicationException,而您只会在编译时看到它们抛出ValidationException..嘿,目前我认为不会太多。我已经考虑过为每个异常类重写build()方法……我认为这种方法和额外的build(exceptionClass)就可以了。
Exception build(Class<? extends Exception> eClass) {
Exception res = null;
try {
res = eClass.newInstance();
} catch (.......) {
// Catch the appropriate exceptions here
}
res.setAbc(); // Set values as needed
...
return res;
}
throw ApplicationException.getBuilder("This is the general error message")
.status(404)
.code(StatusCode.RESOURCE_DOES_NOT_EXIST)
.developerMessage("The resource does not exist")
.buildValidation(); // or buildSomeOther()