0
public void writeList() {
    PrintWriter out = null;

    try {
        System.out.println("Entering" + " try statement");

        out = new PrintWriter(new FileWriter("OutFile.txt"));
        for (int i = 0; i < SIZE; i++) {
            out.println("Value at: " + i + " = " + list.get(i));
        }
    } catch (IndexOutOfBoundsException e) {
        System.err.println("Caught IndexOutOfBoundsException: "
                           +  e.getMessage());

    } catch (IOException e) {
        System.err.println("Caught IOException: " +  e.getMessage());

    } finally {
        if (out != null) {
            System.out.println("Closing PrintWriter");
            out.close();
        } 
        else {
            System.out.println("PrintWriter not open");
        }
    }
}

这个方法的 try 块有三种不同的退出可能性;这是其中的两个。

1) try 语句中的代码失败并抛出异常。这可能是由新的 FileWriter 语句引起的 IOException 或由 for 循环中的错误索引值引起的 IndexOutOfBoundsException。

2)一切顺利,try语句正常退出。

有人可以告诉我,可能发生但此处未提及的第三种潜在可能性是什么?

4

1 回答 1

0

异常可以传播到堆栈中的下一个方法

于 2016-01-28T03:55:45.047 回答