zoukankan      html  css  js  c++  java
  • Java异常处理-----自行处理

    自行处理

    1.try{//可能发生异常的代码 }catch(异常类 变量名){//处理}。
    2.案例除法运算的异常处理。
    3.如果没有进行try catch处理,出现异常程序就停止。进行处理后,程序会继续执行。

    class Demo {
    
        public static void main(String[] args) {
            div(2, 0);
            System.out.println("over");
        }
    
        public static void div(int x, int y) {
    
            try {
                System.out.println(x / y); // 可能出现异常的语句,放入try中。
            } catch (ArithmeticException e) { // 进行异常匹配,
                 //异常信息
                System.out.println(e.toString());
                    System.out.println(e.getMessage()); 
             e.printStackTrace();
                System.out.println("除数不能为0");
            }
            System.out.println("除法运算");
        }
    }

    多个异常

    1.案例print方法,形参中增加数组。
    2.在print方法中操作数组会发生新的异常

    ArrayIndexOutOfBoundsException,NullPointerException),如何处理?

    1.使用将可能发生异常的代码放入try语句中,添加多个catch语句即可。
    2.可以处理多种异常,但是同时只能处理一种异常。
    3.try中除0异常和数组角标越界同时出现,只会处理一种。

    public class Demo{
    
        public static void main(String[] args) {
    
            System.out.println();
            int[] arr = { 1, 2 };
            arr = null;
    
            // print (1, 0, arr);
            print (1, 2, arr);
    
            System.out.println("over");
        }   
    
        public static void print(int x, int y, int[] arr) {
    
            try {
                System.out.println(arr[1]);
                System.out.println(x / y);
            } catch (ArithmeticException e) {
                e.toString();
                e.getMessage();
                e.printStackTrace();
                System.out.println("算术异常。。。");
            } catch (ArrayIndexOutOfBoundsException e) {
                e.toString();
                e.getMessage();
                e.printStackTrace();
                System.out.println("数组角标越界。。。");
            } catch (NullPointerException e) {
                e.toString();
                e.getMessage();
                e.printStackTrace();
                System.out.println("空指针异常。。。");
            }
            System.out.println("函数执行完毕");
        }
    }

    总结

    1.程序中有多个语句可能发生异常,可以都放在try语句中。并匹配对个catch语句处理。
    2.如果异常被catch匹配上,接着执行try{}catch(){} 后的语句。没有匹配上程序停止。
    3.try中多个异常同时出现,只会处理第一条出现异常的一句,剩余的异常不再处理。
    4.使用多态机制处理异常。

    1.程序中多态语句出现不同异常,出现了多个catch语句。简化处理(相当于急诊)。
    2.使用多态,使用这些异常的父类进行接收。(父类引用接收子类对象)

    public static void div(int x, int y, int[] arr, Father f) {
    
            try {
                System.out.println(arr[1]); // 数组越界
                System.out.println(x / y); // 除零
                Son s = (Son) f; // 类型转换
    
            } catch (Exception e) {
                e.toString();
                e.getMessage();
                e.printStackTrace();
                System.out.println("出错啦");
            }
            System.out.println("函数执行完毕");
        }

    多个catch语句之间的执行顺序。

    1.是进行顺序执行,从上到下。
    2.如果多个catch 内的异常有子父类关系。

    1.子类异常在上,父类在最下。编译通过运行没有问题
    2.父类异常在上,子类在下,编译不通过。(因为父类可以将子类的异常处理,子类的catch处理不到)。
    3.多个异常要按照子类和父类顺序进行catch

    class Father {
    
    }
    
    class Son extends Father {
    
    }
    
    public class Demo8 {
    
        public static void main(String[] args) {
    
            System.out.println();
            int[] arr = { 1, 2 };
            arr = null;
            Father f = new Father();
            div(1, 0, arr, f);
    
            System.out.println("over");
        }
    
        public static void div(int x, int y, int[] arr, Father f) {
    
            try {
                System.out.println(arr[1]); 
                System.out.println(x / y);
                Son s = (Son) f;
    
            } catch (ArithmeticException e) {
                e.toString();
                e.getMessage();
                e.printStackTrace();
                System.out.println("算术异常。。。");
            } catch (ArrayIndexOutOfBoundsException e) {
                e.toString();
                e.getMessage();
                e.printStackTrace();
                System.out.println("数组角标越界。。。");
            } catch (NullPointerException e) {
                e.toString();
                e.getMessage();
                e.printStackTrace();
                System.out.println("空指针异常。。。");
            } catch (Exception e) {
                e.toString();
                e.getMessage();
                e.printStackTrace();
                System.out.println("出错啦");
            }
            System.out.println("函数执行完毕");
        }
    }

    总结
    处理异常应该catch异常具体的子类,可以处理的更具体,不要为了简化代码使用异常的父类。

    疑惑:感觉异常没有作用.

    这里写图片描述


    【正在看本人博客的这位童鞋,我看你气度不凡,谈吐间隐隐有王者之气,日后必有一番作为!下面有个“顶”字,你就顺手把它点了吧(要先登录CSDN账号哦 )】


    —–乐于分享,共同进步!
    —–更多文章请看:http://blog.csdn.net/duruiqi_fx


  • 相关阅读:
    hdu4331 Image Recognition 就暴力啊。。啊。。
    [置顶] ASP.Net中服务器控件的生命周期
    Windows下通过脚本快速修改IP地址
    java对象转json应clone,避免生成json串有问题
    Oracle表空间常用操作
    redhat5安装jdk6、eclipse和tomcat6
    Oracle 表的常见操作
    一道来自华为的C机试题目
    [置顶] 获取系统时间的方法--linux
    html5 命运之轮生产
  • 原文地址:https://www.cnblogs.com/hainange/p/6153843.html
Copyright © 2011-2022 走看看