异常01
一、異常對象的產生原因和處理方式
二、異常的拋出
1 public class Demo01 { 2 /* 3 * Throwable:Exception、Error 4 * Exception->RuntimeException 5 * 異常中的關鍵字:throw,在方法內部,拋出異常 6 * 7 * 方法中聲明異常關鍵字 8 * throws用于在方法上的聲明上,標明此方法可能出現異常 9 * 請調用者處理 10 */ 11 public static void main(String[] args) throws Exception{ 12 int[] arr = {}; 13 int i = getArray(arr); 14 System.out.println(i); 15 } 16 public static int getArray(int[] arr)throws Exception{ 17 //方法合法性的判斷 18 if(arr==null){ 19 //拋出異常的形式,告訴調用者 20 //關鍵字throw 21 throw new Exception("傳遞的數組不存在!"); 22 } 23 //對數組進行判斷,判斷數組中是不是有元素 24 if(arr.length==0){ 25 //拋出異常 26 throw new Exception("數組中沒有任何元素!"); 27 } 28 int i = arr[arr.length-1]; 29 return i*2; 30 } 31 }?
轉載于:https://www.cnblogs.com/Nelsoner/p/6701615.html
總結
- 上一篇: python基础——面向对象的程序设计
- 下一篇: 发布功能完成。