数据的异常处理

来源:互联网 发布:echart 引入json地图 编辑:程序博客网 时间:2024/04/29 17:27
package a;import java.util.Scanner;public class c {         static void inputException() throws IllegalAccessException {//定义inputException                  int a[]=new int[4];//实例化,定义包含4个整数型元素的数组                Scanner sc=new Scanner(System.in);//从键盘上输入4个元素的值,并显示                System.out.println("请输入4个整型数据");                    int i,sum=0,avg;                for(i=0;i<4;i++) {                    a[i]=sc.nextInt();                    sum += a[i] ;                }                System.out.println("请输入除数");//输入除数,并用总和除以除数                avg=sc.nextInt();                avg=sum/avg;                System.out.println("avg="+avg);                throw new IllegalAccessException();//抛出异常IllegalAccessException            }            public static void main(String[] args) {                // TODO Auto-generated method stub                c test=new c();//创建属于fj的对象test                try {//为方法Exception()进行监视                    inputException();                }                catch(ArrayIndexOutOfBoundsException e) {//捕获异常,并输入错误提示                    System.out.println("数组越界"+e);                }                catch(ArithmeticException e) {//捕获数学运算异常                    System.out.println("除数不能为0"+e);                }                catch(IllegalAccessException e) {//捕获异常                    System.out.println("非法存取"+e);                }                finally {//最终处理,输出相应信息提示                    System.out.println("最后一定会被执行");                }            }    }

这里写图片描述