Java识别从另一个方法抛出的错误
这第一段代码运行良好,并且没有显示任何警告,因为IDE意识到,如果我们不返回某些内容,我们肯定会抛出异常Java识别从另一个方法抛出的错误,java,spring,exception,design-patterns,coding-style,Java,Spring,Exception,Design Patterns,Coding Style,这第一段代码运行良好,并且没有显示任何警告,因为IDE意识到,如果我们不返回某些内容,我们肯定会抛出异常 private ResponseEntity<String> callService() throws CustomServiceException { // some code here... if(someCondition){ // some code here... return responseVariable }else{ CustomExce
private ResponseEntity<String> callService() throws CustomServiceException {
// some code here...
if(someCondition){
// some code here...
return responseVariable
}else{
CustomException customException = new CustomException(SERVICE_ERROR_MESSAGE, 500);
throw customException;
}
我知道我可以在最后返回null,它永远也不会到达那里,但这是一种不好的做法。像这样的东西有一种常见的做法。在我看来,这样会更清楚(而且它会编译):
在我看来,这样做会更清楚(而且它会编译):
我认为,没有必要使用
handleResponse
打印
throw new CustomException(message, responseCode);
而不是
handleResponse(message, responseCode);
我认为,没有必要使用
handleResponse
打印
throw new CustomException(message, responseCode);
而不是
handleResponse(message, responseCode);
handleResponse(message, responseCode);