相关疑难解决方法(0)

方法签名中的抛出与Java中的抛出语句之间的区别

我试图弄清楚方法签名中的Throws和Java中的Throw Statements之间的区别.方法签名中的引发如下:

public void aMethod() throws IOException{
    FileReader f = new FileReader("notExist.txt");
}
Run Code Online (Sandbox Code Playgroud)

抛出语句如下:

public void bMethod() {
    throw new IOException();
}
Run Code Online (Sandbox Code Playgroud)

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

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

这段代码是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 …
Run Code Online (Sandbox Code Playgroud)

java throw throws

33
推荐指数
2
解决办法
3万
查看次数

标签 统计

java ×1

throw ×1

throws ×1