str=br.readLine();
BufferedReader.readLine()抛出IOException。
public String readLine() throws IOException
抛出:IOException - 如果发生 I/O 错误
由于IOException是一个检查异常,您要么需要使用 try/catch 块处理它,要么使用 throws 子句声明它。
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
str=br.readLine();
i=Integer.parseInt(str);
}catch(IOException e)
{System.out.println("IOException occured... " + e.printStacktrace());
catch(NumberFormatException e)
{System.out.println("enter a valid input");
}
在 java 7 中多次捕获:
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
str=br.readLine();
i=Integer.parseInt(str);
}
catch(IOException | NumberFormatException ex) {
System.out.println(ex.printStackTrace())
}