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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值