我在阅读一本关于异常和断言的Java教材时,遇到了下面这段代码,并有一个问题。
public boolean searchFor(String file, String word)
throws StreamException
{
Stream input = null;
try {
input = new Stream(file);
while (!input.eof())
if (input.next().equals(word))
return true;
return false; //not found
} finally {
if (input != null)
input.close();
}
}
在下一段中,文本说“
searchFor
方法声明它会抛出StreamException
,这样任何生成的异常都会在清理之后传递给调用代码,包括由关闭调用引发的任何StreamException
。”我原以为包含
throws
子句是允许程序员抛出特定类(或子类)异常的方式,只有在throws
子句中包含它或其超类时才能抛出该类。但在这里,try
块中没有throw
语句的throws
子句。那么首先包括该子句的目的是什么?在代码中哪里可以捕获StreamException
异常?