多异常处理
package Exception;
/** 多異常處理* * 1.在聲明異常的時候,建議聲明更為具體的異常,這樣異常可以得到更具體的處理* * 注意:函數(shù)當中只要有異常發(fā)生,該函數(shù)就已經(jīng)結(jié)束了,所以在多個catch的情況下,只可能執(zhí)行一個catch里面的處理代碼*/
public class throwsExceptionS {public static void main(String[] args) {try{div(4,0);}catch(ArithmeticException ae){ //處理算術(shù)異常System.out.println("算術(shù)異常。。。");System.out.println(ae.toString());}catch(ArrayIndexOutOfBoundsException ao){ //處理角標越界異常System.out.println("角標異常。。。");System.out.println(ao.toString()); }}public static int div(int a, int b) throws ArithmeticException,ArrayIndexOutOfBoundsException{int[] arr = new int[a]; //創(chuàng)建一個數(shù)組,長度為傳遞過來的a數(shù)據(jù)System.out.println(arr[5]); //這里一定會發(fā)生數(shù)組角標越界異常return a / b; //這里有可能會發(fā)生算術(shù)異常}
}
轉(zhuǎn)載于:https://www.cnblogs.com/Maple-100/p/3220005.html
總結(jié)
- 上一篇: Central Authenticati
- 下一篇: Spring REST