异常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