zoukankan      html  css  js  c++  java
  • Java学习笔记35(异常)

    代码在运行中发生的问题就是异常

    java中把多种异常封装多个类,当程序出现问题时候,就会创建异常类对象并且抛出相关信息

    异常体系:

    Throwable类是Java中所有错误或异常的父类

    Throwable的子类Error类是所有错误的父类

    Throwable的另一个子类Exception类是所有异常的父类

    在开发中,相对来说,我们并不关心错误,而更关心异常

    异常和错误的区别(通俗解释):

    异常:相当于一个人感冒了,吃药睡觉等进行相应的操作即可痊愈,不修改代码处理掉异常,程序还可以执行

    错误:相当于一个人得了绝症,无法治愈,必须修改代码,程序才可以执行

    所以可以这样理解:我们并不关心患了绝症的人,而会想方法去治愈得了小病的人

    示例:‘

    package demo;
    
    public class Demo {
        public static void main(String[] args) {
            function1();
            function2();
        }
        public static void function1(){
            //异常
            int[] arr = {1,2};
            System.out.println(arr[3]);
            //输出:
            //Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
            //at demo.Demo.function1(Demo.java:11)
            //at demo.Demo.main(Demo.java:5)
        }
        public static void function2(){
            //错误
            int[] arr = new int[666666666];
            System.out.println(arr);
            //输出:
            //Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
            //at demo.Demo.function2(Demo.java:19)
            //at demo.Demo.main(Demo.java:6)
        }
    }

    一旦异常被抛出了,后边的程序都不会执行,因此要想办法解决这个问题

    异常中的关键字throw:

    写一段代码,存在空指针异常

    package demo;
    
    public class ExceptionDemo {
        public static void main(String[] args) {
            int [] arr = null;
            function(arr);
        }
        public static int function(int[] arr){
            int i = arr[arr.length-1];
            return i;
        }
    }

    加入throw关键字,抛出异常:

    package demo;
    
    public class ExceptionDemo {
        public static void main(String[] args) throws Exception {
            int [] arr = null;
            int i = function(arr);
            System.out.println(i);
        }
        public static int function(int[] arr) throws Exception{
            //如果方法中有异常,需要在方法上声明:throws Exception
            //抛出的类型和声明的异常类应当一致
            //main调用这个方法,也需要加入声明
            if(arr == null){
                //抛出异常,告诉调用者
                //手动抛出异常
                throw new Exception("传递数组不存在");
            }
            if(arr.length == 0){
                throw new Exception("传递的数组中没有元素");
            }
            int i = arr[arr.length-1];
            return i;
        }
    }
    /*
    输出:
    Exception in thread "main" java.lang.Exception: 传递数组不存在
    at demo.ExceptionDemo.function(ExceptionDemo.java:16)
    at demo.ExceptionDemo.main(ExceptionDemo.java:6)
    */

    这里注意,如果是运行时异常(RuntimeException),不需要throws声明 ,

    而且如果发生这种异常,那么必须改代码,否则代码无意义

    try  catch方法异常处理:

    package demo;
    
    public class ExceptionDemo {
        public static void main(String[] args) {
            int[] arr = null;
            try {
                int i = function(arr);
                System.out.println(i);
            } catch (NullPointerException ex) {
                System.out.println(ex);
            } catch (ArrayIndexOutOfBoundsException ex) {
                System.out.println(ex);
            }
            System.out.println("结束");
        }
    
        public static int function(int[] arr) throws NullPointerException, ArrayIndexOutOfBoundsException {
            if (arr == null) {
                // 手动抛出空指针异常
                throw new NullPointerException("数组不存在");
            }
            if (arr.length < 6) {
                // 手动抛出数组索引越界异常
                throw new ArrayIndexOutOfBoundsException("数组没有6索引");
            }
            return arr[6];
        }
    }
    /*
    输出:
    java.lang.NullPointerException: 数组不存在
    结束
     */

    这里发现,虽然程序有异常,但是依然执行后面的代码,这就是异常处理

    catch的顺序:

    平级:

    package demo;/*
     * catch处理的注意事项:
     *   有顺序
    * 当异常是平级的时候,即没有继承关系的时候,没有顺序限制
    */ public class ExceptionDemo { public static void main(String[] args) { try { } catch (NullPointerException e) { } catch (ArrayIndexOutOfBoundsException e) { } } }

    有继承关系:

    package demo;
    //有继承关系:越高级的写在越靠后
    public class ExceptionDemo {
        public static void main(String[] args) {
            try {
                
            } catch (NullPointerException ex) {
    
            } catch (RuntimeException ex) {
                
            } catch (Exception ex){
                
            }
        }
    }

    finally:

    package demo;
    
    //finally:必须执行
    //是否有异常都会执行
    public class ExceptionDemo {
        public static void main(String[] args) {
            try {
                function(1);
            } catch (Exception ex) {
                System.out.println(ex);
            } finally {
                System.out.println("代码必须执行");
            }
        }
    
        public static void function(int a) throws Exception {
            if (a == 0) {
                throw new Exception();
            }
            System.out.println(a);
        }
    }
    //无论传入什么,都会输出:代码必须执行

    注意事项:

    1.父类的方法如果抛出异常,子类重写后可以不抛出异常,但是子类如果要抛出异常,这个异常的继承关系不能大于父类的异常

    自定义异常:

    有时候需要一些自定义的异常,这里做一个示例:

    package demo;
    
    public class FuShuException extends RuntimeException {
        public FuShuException(String string){
            super(string);
        }
        public FuShuException(){}
    }
    package demo;
    
    //设置一个情景,计算两门成绩的平均数,不能为负数
    //如果是一个负数,就抛出异常
    public class ExceptionDeno {
        public static void main(String[] args) throws FuShuException {
            try {
                int i = function(10, 97);
                System.out.println(i);
            } catch (FuShuException ex) {
                ex.printStackTrace();
            }
        }
    
        public static int function(int a, int b) throws FuShuException {
            if (a < 0 || b < 0) {
                throw new FuShuException("成绩不为负");
            }
            int sum = a + b;
            return sum / 2;
        }
    }
  • 相关阅读:
    SpringCloud(3)服务消费者(Feign)
    SpringCloud(2)服务消费者(rest+ribbon)
    SpringCloud(1)服务注册与发现Eureka
    SpringBoot中注入ApplicationContext对象的三种方式
    SpringBoot整合Swagger2搭建API在线文档
    SpringBoot整合Shiro使用Ehcache等缓存无效问题
    使用Zint和Zxing实现二维码的编码与解码(Using open-source tools to generate and decode Q-R code)
    简单易懂的现代魔法——Play Framework攻略3
    简单易懂的现代魔法——Play Framework攻略2
    简单易懂的现代魔法——Play Framework攻略1
  • 原文地址:https://www.cnblogs.com/xuyiqing/p/8283373.html
Copyright © 2011-2022 走看看