java中异常处理怎么用的啊?

来源:百度知道 编辑:UC知道 时间:2024/05/24 22:24:13
JAVA中异常处理不是很清楚 到底怎么用的啊?谁能用经典的与语言帮忙概括下啊?

看看例子就会知道怎么用了:
对于有可能抛出异常的代码段放在try{}语句块中并在catch(Exception e){}捕获抛出的异常如下所示:
public static void parse(File file) {
BufferedReader br = null;
boolean comment = false;
try {
String line = "";
br = new BufferedReader(new FileReader(file));
while((line=br.readLine()) != null) {
line = line.trim();
System.out.println("line = " +line);
}

} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}

}
}
对于文件操作可能会出现io异常所以要捕捉

这个很难说清楚!!
只有碰到才知道...!!

想抓就抓,不想抓就抛。。。

异常抛,或者抓,抛用throws,抓用try(){}catch{}finally{}