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

    异常的定义:

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

      异常类:出现问题就会常见异常类对象,并抛出异常的相关信息,异常的位置,原因

    异常体系:

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

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

      Throwable类的子类Exception类是所有异常的父类

      在开发中,更关心异常而不关心错误

    异常和错误的区别:

      异常可以通过开发人员修改代码来处理,让程序继续执行,异常可以被解决,而错误是无法被解决的,也不需要程序员来处理

    示例:

    package com.zs.Demo;
    
    public class Yichang {
        public static void main(String[] args) {
            fun();
            fun2();
        }
    
        private static void fun2() {
            int[] arr=new int[666666666];
            System.out.println(arr);
            /*   异常:
            *Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
            at com.zs.Demo.Yichang.fun(Yichang.java:18)
            at com.zs.Demo.Yichang.main(Yichang.java:5)*/
        }
    
        private static void fun() {
            int[] arr={1,2};
            System.out.println(arr[3]);
            /*错误:
            * Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
            at com.zs.Demo.Yichang.fun(Yichang.java:16)
            at com.zs.Demo.Yichang.main(Yichang.java:5)
            */
        }
    }

        出现异常,那么后边的程序都无法执行,因此 要想办法解决这个问题

      异常的处理方式:

        1:抛出异常:throws

        2:编写try..catch

      throws类:写一段代码,存在空指针异常;

    public class Demo1 {
        public static void main(String[] args) {
            int [] arr=null;
            function(arr);
        }
    
        private static void function(int[] arr) {
            int i=arr[arr.length-1];
            System.out.println(i);
        }
    }

      throws抛出异常:

    public class Demo1 {
        public static void main(String[] args) throws Exception {
            int [] arr=null;
            function(arr);
        }
    
        private static void function(int[] arr) throws Exception {
            if (arr==null)
                throw new Exception("传递数组不存在");
            if (arr.length==0)
                throw new Exception("传递的数组中没有元素");
            int i=arr[arr.length-1];
            System.out.println(i);
            /*Exception in thread "main" java.lang.Exception: 传递数组不存在
            at com.zs.Demo.Demo1.function(Demo1.java:11)
            at com.zs.Demo.Demo1.main(Demo1.java:6)
            */
        }
    }

      如果是运行时异常(RuntimeException),不需要throws声明,必须改代码,否则无意义;

      try...catch异常处理方法:

    package com.zs.Demo;
    
    public class Demo2 {
        public static void main(String[] args) {
            int [] arr=null;
            try {
                function(arr);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("结束");
        }
    
        private static void function(int[] arr) throws Exception {
            if (arr==null)
                throw new Exception("传递数组不存在");
            if (arr.length==0)
                throw new Exception("传递的数组中没有元素");
            int i=arr[arr.length-1];
            System.out.println(i);
            /*java.lang.Exception: 传递数组不存在
            at com.zs.Demo.Demo2.function(Demo2.java:16)
            at com.zs.Demo.Demo2.main(Demo2.java:7)
            结束
            */
        }
    }

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

    finally:

    package com.zs.Demo;
    /*finally:代码必须执行,无论有没有异常都要执行*/
    public class Demo3 {
        public static void main(String[] args) {
            try {
                fun(1);
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                System.out.println("必须执行的代码");
            }
        }
    
        private static void fun(int i) throws Exception {
            if (i==0)
                throw new Exception();
            System.out.println(i);
        }
        /*结果:1
            必须执行的代码      可以看出即使程序没有异常,也会执行finally代码*/
    }

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

    Throwable类的方法:

    /*Throwable类中的方法都和异常信息有关
    * String getMessage() 对异常信息的详细描述
    * String toString() 对异常信息的简短描述
    * void PrintStackTrace()  异常信息最全的jvm默认调用的方法*/
    public class ThrowableDemo {
        public static void main(String[] args) {
            try {
                fun();
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println(e);
                String me = e.getMessage();
                System.out.println(me);
                /*java.lang.Exception: 异常了,下雨了
                at com.zs.Demo.ThrowableDemo.fun(ThrowableDemo.java:19)
                at com.zs.Demo.ThrowableDemo.main(ThrowableDemo.java:9)
                java.lang.Exception: 异常了,下雨了
                异常了,下雨了*/
            }
        }
    
        private static void fun() throws Exception {
            throw new Exception("异常了,下雨了");
        }
    }

      

  • 相关阅读:
    HBase目录
    HBase介绍及简易安装(转)
    Hadoop目录
    通过java读取HDFS的数据 (转)
    Flume-NG一些注意事项(转)
    spark读取 kafka nginx网站日志消息 并写入HDFS中(转)
    Java开发牛人十大必备网站
    HTTP协议状态码详解(HTTP Status Code)(转)
    Flume NG 配置详解(转)
    Android笔记(五)利用Intent启动活动
  • 原文地址:https://www.cnblogs.com/Zs-book1/p/10584577.html
Copyright © 2011-2022 走看看