方法签名中的抛出与Java中的抛出语句之间的区别 [英] Difference between Throws in method signature and Throw Statements in Java

查看:323
本文介绍了方法签名中的抛出与Java中的抛出语句之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图清楚地说明Java中的方法签名 Throw Statements 之间的区别。
方法签名中的抛出如下:

I am trying to make it clear of the difference between Throws in method signature and Throw Statements in Java. Throws in method signature is as following:

public void aMethod() throws IOException{
    FileReader f = new FileReader("notExist.txt");
}

投票声明如下:

public void bMethod() {
    throw new IOException();
}

根据我的理解,抛出是该方法可能抛出此类异常的通知。 throw 语句是在相应情况下实际抛出创建的对象的内容。
从这个意义上讲,如果方法中存在 throw 语句,则应始终显示方法签名中的 throws

From my understanding, a throws in method signature is a notification that the method may throw such an exception. throw statement is what actually throw a created object under according circumstances. In that sense, throws in method signature should always appear if there exist a throw statement in the method.

但是,以下代码似乎没有这样做。代码来自库。我的问题是它为什么会发生?我是否理解错误的概念?

However, the following code doesn't seem doing so. The code is from the library. My question is why it is happening? Am I understanding the concepts wrong?

这段代码是java.util.linkedList的副本。 @author Josh Bloch

This piece of code is a copy from java.util.linkedList. @author Josh Bloch

 /**
 * Returns the first element in this list.
 *
 * @return the first element in this list
 * @throws NoSuchElementException if this list is empty
 */
public E getFirst() {
    final Node<E> f = first;
    if (f == null)
        throw new NoSuchElementException();
    return f.item;
}

答案更新:

更新1:上面的代码与以下相同?

update 1 : is above code the same as the following?

// as far as I know, it is the same as without throws
public E getFirst() throws NoSuchElementException {
    final Node<E> f = first;
    if (f == null)
        throw new NoSuchElementException();
    return f.item;
}

更新2:用于检查异常。我需要在签名中抛出吗?是的。

update 2 : For checked exception. Do I need to have "throws" in the signature? Yes.

// has to throw checked exception otherwise compile error
public String abc() throws IOException{
    throw new IOException();
}


推荐答案

你几乎是对的。除了我要提到的一件事。

You are pretty much right on. Except for one thing I'll mention in a bit.

throws 与方法API一样,也是名称和参数的一部分。客户知道他们是否调用该方法,他们需要处理该异常 - 通过简单地抛出它或者捕获它并处理它(实际上可能需要抛出包装原始的另一个异常)。 throws 在编译时被解决。

throws is as much a part of the method API as the name and the parameters. Clients know if they call that method, they need to handle that exception--by simply throwing it also or by catching it and handling it (which may in fact entail the throwing of another exception wrapping the original). throws is addressed at compile time.

throw 是让运行时知道发生了什么事情的实际行为 - - 事实上我们担心的特殊情况已经发生了。所以它需要在运行时处理。

throw is the actual act of letting the runtime know something bad happened--that the exceptional condition we were worried about has in fact taken place. So it needs to be dealt with at runtime.

但是当你说你不是很正确的时候如果存在一个throw语句,那么方法签名中应该总是出现在方法中。这通常是正确的,但并非总是如此。我还可以调用另一个在我的方法中抛出异常的方法,如果我没有捕获它,我的方法需要抛出它。在这种情况下,我没有明确抛出相同的异常。

But you weren't quite right when you said, "Throws in method signature should always appear if there exist a throw statement in the method." That is often true but not always. I could also call another method that throws an exception within my method, and if I don't catch it, my method needs to throw it. In that case, there is no explicit throw of the same exception by me.

最后一点是你只需要在 throws中声明一个异常当异常是 checked 异常时 - 意味着它来自RuntimeException的Exception类层次结构的另一端。常见的已检查异常是IOException和SQLException。如果您不自行处理,则必须在方法签名的throws部分中列出已检查的异常。任何继承RuntimeException的东西 - 比如你的例子中的NoSuchElementException以及讨厌的NullPointerException - 是一个未经检查的异常,不必被捕获或抛出或任何东西。

The final point is that you only need to declare an exception in throws when the exception is a checked exception--meaning it is from the other side of the Exception class hierarchy from RuntimeException. Common checked exceptions are IOException and SQLException. Checked exceptions must be listed in the throws part of the method signature if you don't handle them yourself. Anything subclassing RuntimeException--like NoSuchElementException in your example and also the hated NullPointerException--is an unchecked exception and doesn't have to be caught or thrown or anything.

通常,您使用已检查的异常来解决可恢复的问题(客户端知道会发生什么,可以优雅地处理问题并继续前进)和未经检查的灾难性异常(例如无法连接到数据库)。

Typically, you use checked exceptions for recoverable problems (where the client knows what can happen and can gracefully handle the problem and move on) and unchecked exceptions for catastrophic problems (like can't connect to the database).

如果你可以通过所有AOP的东西,这个是关于如何有效使用已检查和未经检查的例外的很好的讨论。

If you can get past all the AOP stuff, this is a great discussion of how you use checked and unchecked exceptions effectively.

这篇关于方法签名中的抛出与Java中的抛出语句之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆