抛出语句在java中是什么意思?
另外,抛出语句在java中是什么意思?,java,Java,另外,抛出NumberFormatException,IOException意味着什么?我一直试图使用BufferedReader,我说 BufferedReader nerd = new BufferedReader(new InputStreamReader(System.in)); 但是除非抛出NumberFormatException,否则BufferedReader将无法工作,IOException被放入。Throws子句用于声明未由特定方法处理的异常,并指示调用方显式处理这些异常或
抛出NumberFormatException,IOException
意味着什么?我一直试图使用BufferedReader,我说
BufferedReader nerd = new BufferedReader(new InputStreamReader(System.in));
但是除非
抛出NumberFormatException,否则BufferedReader
将无法工作,IOException
被放入。Throws子句用于声明未由特定方法处理的异常,并指示调用方显式处理这些异常或在调用层次结构中重新调用它们。Throws子句用于声明未由特定方法处理的异常,并且是指示调用方显式处理这些异常或在调用层次结构中重新调用它们。关键字throws
表示某个方法可能“抛出”某个异常。您需要使用try-catch
块或向方法声明中添加throws-IOException,(…)
来处理可能的IOException
(以及可能的其他异常)。大概是这样的:
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
关键字表示某个方法可能会“抛出”某个异常。您需要使用try-catch
块或向方法声明中添加throws-IOException,(…)
来处理可能的IOException
(以及可能的其他异常)。大概是这样的:
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语句表示函数可能“抛出”错误。Ie抛出一个将结束当前方法的错误,并使堆栈上的下一个“try-catch”块处理它 在这种情况下,您可以将“throws…”添加到方法声明中,也可以执行以下操作:
try {
// code here
} catch (Exception ex) {
// what to do on error here
}
阅读了解更多信息。throws语句表示函数可能“抛出”错误。Ie抛出一个将结束当前方法的错误,并使堆栈上的下一个“try-catch”块处理它 在这种情况下,您可以将“throws…”添加到方法声明中,也可以执行以下操作:
try {
// code here
} catch (Exception ex) {
// what to do on error here
}
阅读了解更多信息。您是否查看了文档?是否想过在try-catch中计算代码,然后在那里逐个捕获这些异常[如果JDK你先考虑阅读手册??这里:可能的副本你检查过文档吗?曾经想过在尝试catch中填充代码,然后逐个捕获这些异常。[如果JDK您首先考虑阅读手册??这里:可能的副本“代码< >代码异常> /代码>是否需要在任何地方被定义?如果它是一个对象,它必须有一个<代码>类< /代码>声明权?是否需要在任何地方定义<代码> IOExtUs>代码?如果它是一个对象,它必须有一个<代码>类< /代码>声明RI。嗯?