鱼欲遇雨:每日都学习一点,持之以恒,天道酬勤!不能用电脑时,提前补上!(2012.9.6)
java的例外处理机制使得例外事件,沿着被调用的顺序往前寻找,只要找到符合该例外种类的例外处理程序。
//TestEx.java
import java.io.*;
public class TestEx {
public static void main(String args[]) {
//TestEx te = new TestEx();
//te.f2(); //throws
try
{
new TestEx().f2();
}
catch (IOException e)
{
e.printStackTrace();
}
/* int[] arr = {1, 2, 3};
System.out.println(arr[2]);
try
{
System.out.println(2/0);
}catch (ArithmeticException oe)
{
System.out.println("系统异常");
oe.printStackTrace();
}
*/
//TestEx te = new TestEx();
//te.m(0);
/*
try
{
new TestEx().m(0);
}
catch (ArithmeticException te)
{
te.printStackTrace();
System.out.println("出错啦");
}
*/
FileInputStream in = null;
try{
in = new FileInputStream("myfile.txt");
int b;
b = in.read();
while(b != -1) {
System.out.println((char)b);
b = in.read();
}
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
System.out.println(e.getMessage());
}finally{
try{
in.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
void m(int i) throws ArithmeticException {
if(i == 0)
throw new ArithmeticException("被除数为0");
}
void f() throws FileNotFoundException, IOException{
FileInputStream in = new FileInputStream("myfile.txt");
int b;
b = in.read();
while(b != -1) {
System.out.println((char)b);
b = in.read();
}
}
void f2() throws IOException {
/*
try{
f();
}catch (FileNotFoundException e){
System.out.println(e.getMessage());
}catch (IOException e){
e.printStackTrace();
}
*/
//也不知道怎么处理f()的异常
f();
}
}
这一章的主要内容:“一个图,五个关键字” try,catch,finally, throw, throws