简体   繁体   English

Java:声明未经检查的异常的最佳实践

[英]Java: best practice for declaring unchecked exception

Let's say you have some method declaring throwing an unchecked exception 假设您有一些声明抛出未经检查的异常的方法

EDIT : let's say it's not you who designed this method, but authors of a very respectable framework (Spring, aekhm!), you're just calling it 编辑 :让我们说这不是你设计这个方法的人,而是一个非常可敬的框架的作者(Spring,aekhm!),你只是称它为

void someMethod() throws UncheckedException;

My first question is: 我的第一个问题是:

  1. Is there any reason other than clarity for declaring that unchecked exception in the throws clause? 除了在throws子句中声明未经检查的异常之外,还有其他原因吗?

Let's say you have another method that's calling someMethod 假设您有另一种调用someMethod方法

void someOtherMethod() {
    someMethod()
}

My second question is: 我的第二个问题是:

  1. What would be the best practice of deciding whether to declare throwing the UncheckedException in the someOtherMethod ? 决定是否在someOtherMethod声明抛出UncheckedException的最佳做法是什么?

Just a little background: 只是一点背景:

Spring framework's exception are based on unchecked exceptions. Spring框架的异常基于未经检查的异常。 So for example some methods are throwing (and declaring it in the throws ) DataAccessException. 因此,例如,某些方法抛出(并在throws声明它)DataAccessException。 If my code is using these calls, should it or should it not declare throwing these exceptions? 如果我的代码正在使用这些调用,它是否应该声明抛出这些异常? And why? 为什么?

There is no need to declare unchecked exceptions in the throws ... clause of a method. 无需在方法的throws ...子句中声明未经检查的异常。 As stated in the Java Language Specification , " It is permitted but not required to mention unchecked exception classes in a throws clause ". 正如Java语言规范中所述 ,“ 允许但不要求在throws子句中提及未经检查的异常类 ”。

It is common practice to list unchecked exceptions in your Javadoc if you anticipate that users of your API might encounter them. 如果您预计API的用户可能会遇到它们,则通常会在Javadoc中列出未经检查的异常。 A common example of this is to list why an IllegalArgumentException might be thrown. 一个常见的例子是列出可能抛出IllegalArgumentException原因。

If you wrap one method with another, apply the Effective Java principle of throwing exceptions suitable to the level of abstraction (Item 61). 如果用另一个方法包装一个方法,则应用有效Java原则来抛出适合抽象级别的异常(第61项)。 This should be applied to both checked exceptions and expected unchecked exceptions. 这应该应用于已检查的异常和预期的未经检查的异常。

I recommend reading chapter 9 in the great book "Effective Java". 我建议阅读伟大的书“Effective Java”中的第9章。 You will get all the answers to your questions and you will enjoy a great reading. 您将获得所有问题的答案,您将获得很好的阅读。

Specifically for you questions: 专门为您提问:

Use the Javadoc @throws tag to document each unchecked exception that a method can throw, but do not use the throws keyword to include unchecked exceptions in the method declaration 使用Javadoc @throws标记来记录方法可以抛出的每个未经检查的异常,但不要使用throws关键字在方法声明中包含未经检查的异常

Best practice is to not declare unchecked exceptions at all. 最佳做法是不要声明未经检查的例外。 You don't see methods like this: 你没有看到这样的方法:

public void foo() throws NullPointerException {...}

You should only use throws for Checked Exceptions . 您应该仅对Checked Exceptions使用throws。 By saying throws you expect clinet to handle his Exception in any way and this is not true for Unchecked Exceptions 通过说throws你希望clinet以任何方式处理他的异常,而对于Unchecked Exceptions则不然

EDIT: 编辑:

There has been some lively discussion in comments so just to clarify: you don't have to declare Unchecked Exceptions which doesn't mean you cannot, although in most cases you shouldn't. 在评论中有一些热烈的讨论,所以只是为了澄清:你不必声明Unchecked Exceptions ,这并不意味着你不能,尽管在大多数情况下你不应该。 I prefer to mention them in javadoc style comments rather than in throws clause. 我更喜欢在javadoc样式注释中而不是在throws子句中提及它们。 There is plenty other examples where you generally shouldn't do something but in some cases you may need to. 还有很多其他例子,你通常不应该做某些事情,但在某些情况下你可能需要这样做。 In my opinion the bottom line is: no, you shouldn't be listing Unchecked Exceptions in throws clause . 在我看来,底线是:不,你不应该在throws clause列出Unchecked Exceptions

To my mind, best practice is to not use throws in the head of the method for unchecked exception, but don't forget to document every exception (checked or not) thrown by your method : 在我看来,最好的做法是不要在方法的头部使用throws来进行未经检查的异常,但是不要忘记记录你的方法抛出的每个异常(已检查或未检查):

/**
 * Some doc
 * @throws IllegalArgumentException if ...
 */
 public void m() {
     //...
     throw new IllegalArgumentException();
 }

Documenting your API is a good thing and either declaring a throws or adding @throws to your javadoc is good practice. 记录您的API是一件好事,无论是声明抛出还是将@throws添加到您的javadoc都是一种很好的做法。 It just makes things more explicit for the reader, which is never a bad thing. 它只会让读者更清楚,这绝不是坏事。 I don't always do this but, generally, you want people to be aware that something might fail. 我并不总是这样做,但一般来说,你希望人们意识到某些事情可能会失败。 I wouldn't do this for NPEs but things like input validation errors or authorization errors are things you might want to document. 我不会为NPE执行此操作,但输入验证错误或授权错误等内容是您可能要记录的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM