码迷,mamicode.com
首页 > 其他好文 > 详细

异常01

时间:2017-04-12 23:30:19      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:images   error   合法性   main   null   com   []   except   throwable   

一、异常对象的产生原因和处理方式

技术分享

二、异常的抛出

 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 }

 

异常01

标签:images   error   合法性   main   null   com   []   except   throwable   

原文地址:http://www.cnblogs.com/Nelsoner/p/6701615.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!