throws语句在java中意味着什么? [英] What does throws statement mean in java?
本文介绍了throws语句在java中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
另外,会抛出NumberFormatException,IOException
是什么意思?我一直试图使用 BufferedReader
说:
Also, what does throws NumberFormatException, IOException
mean? I keep trying to use BufferedReader
by saying
BufferedReader nerd = new BufferedReader(new InputStreamReader(System.in));
但 BufferedReader
除非抛出NumberFormatException,IOException
被放入。
推荐答案
throws
keyword表示某个方法可能会抛出某个异常。您需要使用 try-catch
块或添加<来处理可能的 IOException
(以及可能的其他异常) code>向您的方法声明抛出IOException,(...)。这样的事情:
The throws
keyword indicates that a certain method can potentially "throw" a certain exception. You need to handle a possible IOException
(and possibly other exceptions) either with a try-catch
block or by adding throws IOException, (...)
to your method declaration. Something like this:
public void foo() throws IOException /* , AnotherException, ... */ {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
in.readLine();
// etc.
in.close();
}
public void foo() {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try {
in.readLine();
// etc.
in.close();
} catch (IOException e) {
// handle the exception
} /* catch (AnotherException e1) {...} ... */
}
这篇关于throws语句在java中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文