Java 在switch语句中返回异常,然后抛出被调用的方法
我有两种方法Java 在switch语句中返回异常,然后抛出被调用的方法,java,exception,switch-statement,throw,Java,Exception,Switch Statement,Throw,我有两种方法 private String handleResponse(HttpResponse httpResponse){ if (response.getStatusCode() / 100 == 2) { return response.getEntity().toString() } else { throw handleException(response.getStatusCode()); } } 一切正常,但这种方法正确
private String handleResponse(HttpResponse httpResponse){
if (response.getStatusCode() / 100 == 2) {
return response.getEntity().toString()
} else {
throw handleException(response.getStatusCode());
}
}
一切正常,但这种方法正确吗?我的意思是从方法中的开关返回新的RuntimeException,然后抛出整个方法?有些东西告诉我它不是,我想知道为什么以及如何改进它。您可以通过以下方式使它更简短、更直观:
private String handleResponse(HttpResponse httpResponse){
if (response.getStatusCode() == 200) {
return response.getEntity().toString()
} else {
throw new RuntimeException(getErrorMessage(response.getStatusCode());
}
}
private String getErrorMessage(int errorStatucCode){
switch(errorStatucCode) {
case 400:
return "Invalid request";
case 401:
return "User not authorized";
default:
return "Unkown exception";
}
您所拥有的没有任何问题,但您可能会发现在创建异常时立即抛出异常更为自然:
private String handleResponse(HttpResponse httpResponse){
if (response.getStatusCode() / 100 == 2) {
return response.getEntity().toString()
} else {
throwException(response.getStatusCode());
}
}
private void throwException(int errorStatusCode){
switch(errorStatusCode) {
case 400:
throw new RuntimeException("Invalid request");
case 401:
throw new RuntimeException("User not authorized");
default:
throw new RuntimeException("Unkown exception");
}
}
我调用的API可以作为成功响应返回200201204。但我愿意接受任何建议 如果您接受任何2xx状态码,那么:
if (response.getStatusCode() >= 200 && response.getStatusCode() <= 299) {
if(response.getStatusCode()>=200&&response.getStatusCode()
去掉response.getStatusCode()/100==2
。改为写入response.getStatusCode()==200
或response.getStatusCode()==HttpStatus.SC\u OK
删除else
分支,并在if
语句之后抛出
将该方法重命名为getExceptionByStatusCode
或generateExceptionForStatusCode
。如果不handleException
,则由您决定抛出哪个方法
选择正确的返回类型。不要使用RuntimeException
。它可以是ResponseStatusException
,也可以是适合HTTP/your域抽象的任何其他类型
对于每种情况,决定要返回的类型。同样,不要RuntimeException
稍微改进一点的版本就可以了
private String handleResponse(HttpResponse response) {
final int statusCode = response.getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
return response.getEntity().toString();
}
throw getExceptionByStatusCode(statusCode);
}
private MyDomainHTTPException getExceptionByStatusCode(int statusCode) {
switch (statusCode) {
case HttpStatus.SC_NOT_FOUND:
return new MyDomainHTTPException("...");
case HttpStatus.SC_UNAUTHORIZED:
return new MyDomainHTTPException("...");
default:
return new MyDomainHTTPException("...");
}
}
也就是说,在Java中返回一个异常感觉是不对的
它工作正常,但由于异常的“特殊”状态,它似乎不是绝对正确的。当存在延迟计算时,它是合理的,并且在将来满足某个条件时,或者在使用Optional.orelsetrow
的情况下,您将抛出异常
对于一些习惯于抛出新的
模板并且从未想过返回异常是可编译选项的人来说,这可能会让他们感到困惑
在大型框架(Spring、PrimeFaces——如果我没记错的话)中,我看到异常工厂用于根据给定的上下文和规则组合异常。它们肯定比我们更广泛地使用异常。因此,你可以忽略我的感受;)为什么不让您的
handleException
立即抛出异常,而不是返回异常?另外,由于您的代码目前正在运行,您只是在寻求建议,我相信这个问题更多的是针对StackExchange论坛。然后从交换机返回消息,并在callsight处抛出异常。@AnisR。我很想这样做,但Idea抱怨说,else分支也应该返回一些语句response.getStatusCode()/100==2
Dude,真的吗?是否为弱者提供response.getStatusCode()==200
?我也在考虑这个选项,但是如果我曾经需要使用Throwable使用另一个构造函数创建RuntimeException呢?例如抛出新的RuntimeException(getErrorMessage(response.getStatusCode(),cause);
它编译。由于默认值的大小写,所有路径都被覆盖。我将返回字符串
并调用方法getErrorMessageByStatusCode()
。它删除了重复项,允许您在创建时抛出,通常会使代码更加合理。@boristspider您可能希望返回不同(更具描述性)类型和不同数量的参数
private String handleResponse(HttpResponse response) {
final int statusCode = response.getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
return response.getEntity().toString();
}
throw getExceptionByStatusCode(statusCode);
}
private MyDomainHTTPException getExceptionByStatusCode(int statusCode) {
switch (statusCode) {
case HttpStatus.SC_NOT_FOUND:
return new MyDomainHTTPException("...");
case HttpStatus.SC_UNAUTHORIZED:
return new MyDomainHTTPException("...");
default:
return new MyDomainHTTPException("...");
}
}