【发布时间】:2015-09-10 10:03:43
【问题描述】:
我知道异常发生在运行时而不是编译时。
所以这段代码编译成功,没有任何编译时java.lang.ArithmeticException: / by zero类型的异常,但只在运行时给出异常
class divzero{
public static void main(String[] arg){
System.out.print(3/0);
}
}
但是当我使用BufferedReader 类时,当我编译下面给出的代码时它会说“unreported exception java.io.IOException; must be caught or declared to be thrown”。我认为它应该在运行时而不是在编译时产生此异常。因为异常发生在编译时。
import java.io.*;
class Bufferedreaderclass{
public static void main(String[] arg)
{
System.out.print("mazic of buffer reader \n Input : ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input;
input = br.readLine();
System.out.print("Your input is: "+input);
}
}
我知道,我应该抛出 IOException 类型的异常,但为什么呢?我想知道为什么“unreported exception java.io.IOException”在编译时而不是在运行时。
请告诉我为什么会这样?
【问题讨论】:
-
不是异常,而是语法错误。请使用
try...catch或throws子句。
标签: java exception compilation bufferedreader ioexception