throws语句在java中意味着什么?

还有什么会throws NumberFormatException, IOException是什么意思? 我一直在尝试使用BufferedReader

 BufferedReader nerd = new BufferedReader(new InputStreamReader(System.in)); 

但是BufferedReader将无法工作,除非throws NumberFormatException, IOException放入throws NumberFormatException, IOException

Throws子句用于声明未由特定方法处理的exception,并且是指示调度程序显式处理这些exception或在调用层次结构中重新抛出它们的指令。

throws关键字表示某种方法可能会“抛出”某个exception。 您需要使用try-catch块或通过向方法声明添加throws IOException, (...)来处理可能的IOException (以及可能的其他exception)。 像这样的东西:

 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 ….’添加到方法声明中,也可以执行以下操作:

 try { // code here } catch (Exception ex) { // what to do on error here } 

阅读http://docs.oracle.com/javase/tutorial/essential/exceptions/了解更多信息。