利用try~catch语句来捕获异常


public class TestException {                               //创建类
public static void main(String[]args){
String[] ss={"aa","bb","cc","dd"};
try{                             //try语句中包含可能出现的异常代码
for(int i=0;i<ss.length;i++){
System.out.println(ss[i]);
int result=5/0;
System.out.println(result);
String s=null;
boolean b=s.equals("aaaa");
}
}
catch(NullPointerException e){     //catch中用来获取异常信息
System.out.println("1");
}
catch(java.lang.ArithmeticException e){
System.out.println("2");
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("3");
}
catch(Exception e){
System.out.println("4");
}
finally{
System.out.println("finally");
}
System.out.println("end");
}
}

猜你喜欢

转载自sujinghui1024.iteye.com/blog/2243349