英文:
Getting a warning for using throws key word on my function header
问题
这是一个方法,我抛出了一个名为"Data Exception"的异常,所以我需要在函数头部添加"throws DataException",对吗?但是当我添加后,IDE会显示一个警告:"在该方法中从不抛出com.DataException",但是我明明在两个地方抛出了DataException,想知道我在这里做了什么错误。非常感谢任何帮助。
public ResponseEntity<?> uploadExcelFile(@RequestParam("file") final MultipartFile file) throws DataException //在这里获取了要移除的警告。
{
try
{
if (NullEmptyUtils.isNull(file) || file.isEmpty())
{
throw new DataException(StringConstants.EXCEPTION, StringConstants.FILE_IS_NULL_OR_EMPTY,
HttpStatus.BAD_REQUEST);
}
if (ExcelHelper.hasExcelFormat(file))
{
excelService.save(file, getLoggedInUser());
}
else
{
throw new DataException(StringConstants.EXCEPTION, StringConstants.FILE_IS_NOT_AN_EXCEL_FORMAT,
HttpStatus.BAD_REQUEST);
}
}
catch (DataException e)
{
logger.error(StringConstants.EXCEPTION, e);
return buildError(new DataException("error", "上传文件时发生错误", HttpStatus.INTERNAL_SERVER_ERROR));
}
return buildResponse("已上传文件 ->" + file.getOriginalFilename());
}
}
英文:
this is a method where i am throwing an exception called "Data Exception",so i have to add "throws DataException" to the function header right?But when I add it, i get a warning(From IDE) saying that -"com.DataException is never thrown in the method",but i am clearly throwing the DataException right(in 2 places)?Want to know what mistake i am doing here.Any help appreciated.
public ResponseEntity<?> uploadExcelFile( @RequestParam( "file" ) final MultipartFile file ) throws DataException //Getting the warning to remove here.
{
try
{
if( NullEmptyUtils.isNull(file) || file.isEmpty() )
{
throw new DataException(StringConstants.EXCEPTION, StringConstants.FILE_IS_NULL_OR_EMPTY,
HttpStatus.BAD_REQUEST);
}
if( ExcelHelper.hasExcelFormat(file) )
{
excelService.save(file, getLoggedInUser());
}
else
{
throw new DataException(StringConstants.EXCEPTION, StringConstants.FILE_IS_NOT_AN_EXCEL_FORMAT,
HttpStatus.BAD_REQUEST);
}
}
catch( DataException e )
{
logger.error(StringConstants.EXCEPTION, e);
return buildError(new DataException("error", "Error occurred while uploading file",
HttpStatus.INTERNAL_SERVER_ERROR));
}
return buildResponse("Uploaded file ->" + file.getOriginalFilename());
}
}
答案1
得分: 2
throws
子句用于在方法运行时可能抛出但在方法内部没有显式捕获的异常。在您的情况下,您确实抛出了一个DataException
,但接着又有一个catch (DataException e)
,它捕获了这个异常。因此,异常从未被抛出出该方法,这意味着您不需要声明它。
只有在以下情况下需要在throws
子句中声明异常:
- 在方法内部抛出(或在方法调用的内容中抛出)
- 在方法内部没有捕获
- 不是
RuntimeException
的子类型。
您应该避免声明不满足这些条件的任何异常。这就是编译警告告诉您的内容。
英文:
The throws
clause is for exceptions that might get thrown while your method is running, but aren't explicitly caught within your method. In your case, you're certainly throwing a DataException
, but then you have a catch (DataException e)
, which catches the exception. So the exception is never thrown out of the method, which means that you don't have to declare it.
You only have to declare an exception in a throws
clause if it's
- thrown within the method (or in something called by the method)
- not caught within the method
- not a subtype of
RuntimeException
.
and you should avoid declaring any exceptions that don't meet these criteria. That's what the compile warning is telling you.
答案2
得分: 2
记住针对已检查异常的"捕获或声明"规则,该规则如下:
- 如果你的方法中的代码抛出了异常,那么要么
- 在你的方法内部捕获异常,方法内把预计会抛出异常的代码用 try-catch 块包围起来,要么
- 在你的方法签名中声明该异常,通过使用 throws 子句,这样调用你的方法的调用方就会知道你的方法可能会抛出这个异常,并可以进行处理。
在你的情况下,由于你已经在方法中捕获了异常,所以不需要添加 throws 子句。
英文:
Remember the catch or declare rule for checked Exceptions, which says that :
- If code in your method is throwing an exception, then either
- catch the exception in your method in by surrounding the code which is expected to throw an exception, by try-catch block OR
- declare the exception in your method signature by using throws clause, so that caller of your method knows that your method can throw this exception and can handle.
In your case, as you are already catching exception in your method, you don't need to add in a throws clause.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论