异常处理
package com.softeem.len11;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.SocketException;
public class Test {
//申明此方法有可能会抛出一个空指针异常
public void show(String param) throws SocketException , IOException ,Exception{
int number=param.length();//得到字符串长度
System.out.println("字符串长度"+number);
} //申明泡池一个已检查异常(文件没找到)
public void readFile(String filePath)throws FileNotFoundException {
}
public void tets()throws FileNotFoundException{
this.readFile("c:/123.txt");
}
//throws--声明异常 throw-----抛出异常对象
public void test2(int i,int j)throws ArithmeticException{//运行时异常
if (j==0) {
ArithmeticException ex=new ArithmeticException("除数不能为零");
throw ex;//抛出异常对象
}
int x=i/j;
System.out.println("结果:"+x);
}
public static void main(String[] args){
Test t=new Test();
try {
t.tets();
t.show(null);//方法调用
t.readFile("c:/123.txt");//方法调用
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t.test2(4, 0);
// FileInputStream in=new FileInputStream("c:123.txt");
}
}