import java.io.IOException;
public class Test3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//
// method3();
// method4();
// try {
method5(10,1);
// } catch (IOException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
// }
}
// 手動(dòng)拋異常使用throw
// throws 與 throw 區(qū)別
public static int method5(int a,int b)? {
if(b==0) {
throw new RuntimeException();
}else if(b==1) {
// throw new IOException();
}
return a/b;
}
public static void method4() {
System.out.println("open");
try {
System.out.println(10/0);
// return ;
// System.exit(0);
}catch(Exception e){
System.out.println(e);
}finally {
System.out.println("close");
}
}
// 捕獲不同種類的異常時(shí) 有兩種解決方案
// 1.寫多個(gè)catch?
// 2.寫Exception 可以捕獲其他所有異常
// 3.在jdk1.7以后 單個(gè)catch可以捕獲多個(gè)異常 (不能出現(xiàn)父子關(guān)系)
// 寫多個(gè)catch時(shí)注意 子類寫在前,父類寫在后
public static void method3() {
//
Person p=new Person();
p=null;
try {
// p.say();
System.out.println(10/0);
}
catch(ArithmeticException|NullPointerException e) {? //ArithmeticException
System.err.println("捕獲到了異常"+e);
}catch(Exception e) {
System.err.println("父類可以捕獲子類異常");
}
//NullPointerException
}
public static void method1() {
// Person p=new Person();
Person p=null;
p.say();
}
public static void method2() {
method1();
}
}