Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java识别从另一个方法抛出的错误_Java_Spring_Exception_Design Patterns_Coding Style - Fatal编程技术网

Java识别从另一个方法抛出的错误

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

这第一段代码运行良好,并且没有显示任何警告,因为IDE意识到,如果我们不返回某些内容,我们肯定会抛出异常

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