异常的学习

异常:代表程序中可能出现的问题

异常的分类:

Error:代表系统的级别错误(属于严重问题)系统一旦出现问题,sun公司会把这些错误封装成Error对象。Error是给sun公司自己用的,不是给我们程序员用的。因此我们开发人员不用管它。

Exception(父类):叫做异常,代表程序可能出现的问题。我们通常会用Exception以及它的子类来封装程序出现的问题。

         编译时异常:没有继承RuntimeException的异常,直接继承于Exception。编译阶段就会出现异常提醒的,如:日期解析异常。(Java文件--->字节码文件)

        运行时异常(RuntimeException):RuntimeException及其子类。编译阶段不会出现异常提醒,运行时出现的异常,一般是由于参数传递错误带来的问题,如:数组索引越界异常。(字节码文件--->运行结果)

异常的作用:

        作用1:异常是用来查询bug的关键参考信息

        作用2:异常可以作为方法内部的一种特殊返回值,以便通知调用者底层的执行情况

异常的处理方式:

        方式1:JVM默认的处理方式。把异常的名称,异常的原因及异常出现的位置等信息输出在了控制台。(红色字体)

       方式2:自已处理( 捕获异常 )。

        try{

                可能出现异常的代码;

        }catch(  异常类名  变量名 ) {

                异常的处理代码;

        }

        目的:当代码出现异常时,可以让程序继续往下执行,一般用在调用处。

int[] arr = {1, 2, 3, 4, 5, 6};
        try{
            //可能出现异常的代码;
            System.out.println(arr[10]);//此处出现了异常,程序就会在这里创建一个ArrayIndexOutOfBoundsException对象
                                        //new ArrayIndexOutOfBoundsException();
                                        //拿着这个对象到catch的小括号中对比,看括号中的变量是否可以接收这个对象
                                        //如果能被接收,就表示该异常就被捕获(抓住),执行catch里面对应的代码
                                        //当catch里面所有的代码执行完毕,继续执行try...catch体系下面的其他代码
        }catch(ArrayIndexOutOfBoundsException e){
            //如果出现了ArrayIndexOutOfBoundsException异常,我该如何处理
            System.out.println("索引越界了");
        }
        System.out.println("看看我执行了吗?");
             /*运行结果:
            * 索引越界了
              看看我执行了吗?*/

         问1:如果try中没有遇到问题,怎么执行?

         会执行try中所有代码,不会执行catch里面的代码,只有出现异常才执行catch里的所有代码

  int[] arr = {1, 2, 3, 4, 5, 6};

        try{
            System.out.println(arr[0]);//1
        }catch(ArrayIndexOutOfBoundsException e){
            System.out.println("索引越界了");
        }
        System.out.println("看看我执行了吗?");//看看我执行了吗?
        /*运行结果:
            1
            看看我执行了吗?*/

         问2:try中遇到多个问题,怎么处理?

         写多个catch与之对应,如果要捕获多个异常,这些异常如果存在父子关系的话,那么父类一定要写在下面。

        在JDK7之后,可以在catch中捕获多个异常,中间用 “ | ”进行隔开,表示如果出现了A异常或者B异常的话,采取同一种处理方案。

//JDK7
        int[] arr = {1, 2, 3, 4, 5, 6};

        try{
            System.out.println(arr[10]);//ArrayIndexOutOfBoundsException
            System.out.println(2/0);//ArithmeticException
            String s = null;
            System.out.println(s.equals("abc"));
        }catch(ArrayIndexOutOfBoundsException | ArithmeticException e){
            System.out.println("索引越界了");
        }catch(NullPointerException e){
            System.out.println("空指针异常");
        }catch (Exception e){
            System.out.println("Exception");
        }

        System.out.println("看看我执行了吗?");
        /*运行结果:
            索引越界了
            看看我执行了吗?*/

         问3:try中遇到的问题没有被捕获,怎么处理?

         相当于try...catch代码白写了,最终会交给虚拟机进行处理。

int[] arr = {1, 2, 3, 4, 5, 6};

        try{
            System.out.println(arr[10]);//new ArrayIndexOutOfBoundsException();
        }catch(NullPointerException e){
            System.out.println("空指针异常");
        }

        System.out.println("看看我执行了吗?");
        /*运行结果:索引越界异常
        Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 6
	at com.itheima.a01myexception.ExceptionDemo9.main(ExceptionDemo9.java:16)*/

         问4:如果try中遇到了问题,try下面的代码还会执行吗?

         不会执行,直接跳转到对应的catch当中,执行catch里面的语句体,如果没有对应的catch与之匹配,那么还是会交给虚拟机进行处理

int[] arr = {1, 2, 3, 4, 5, 6};

        try{
            System.out.println(arr[10]);
            System.out.println("看看我执行了吗?... try");
        }catch(ArrayIndexOutOfBoundsException e){
            System.out.println("索引越界了");
        }
        System.out.println("看看我执行了吗?... 其他代码");
        /*运行结果:
        *索引越界了
         看看我执行了吗?... 其他代码 */

        异常中的常见方法:

        public String getMessage()       

        返回此 throwable 的详细消息字符串

int[] arr = {1, 2, 3, 4, 5, 6};

        try {
            System.out.println(arr[10]);
        } catch (ArrayIndexOutOfBoundsException e) {
            String message = e.getMessage();
            System.out.println(message);//Index 10 out of bounds for length 6
        }
        System.out.println("看看我执行了吗?");
        /*运行结果:
        * Index 10 out of bounds for length 6
          看看我执行了吗?*/

        public String toString()            

        返回此可抛出的简短描述

int[] arr = {1, 2, 3, 4, 5, 6};

        try {
            System.out.println(arr[10]);
        } catch (ArrayIndexOutOfBoundsException e) {
            String str = e.toString();
            System.out.println(str);
        }
        System.out.println("看看我执行了吗?");
        /*运行结果:
        * java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 6
          看看我执行了吗?*/

        public void printStackTrace()      

        在底层是利用System.err.println进行输出,把异常的错误信息以红色字体输出在控制台
        细节:仅仅是打印信息,不会停止程序运行

int[] arr = {1, 2, 3, 4, 5, 6};

        try {
            System.out.println(arr[10]);
        } catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();//仅仅打印信息,不会停止运行
        }
        System.out.println("看看我执行了吗?");
        /*运行结果:
        * java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 6
	at com.itheima.a01myexception.ExceptionDemo11.main(ExceptionDemo11.java:46)
          看看我执行了吗?*/

        扩展:System.out 与 System.err区别,了解就行。

            //正常的输出语句
            System.out.println(123);

            //错误的输出语句(而是用来打印错误信息)
            System.err.println(123);//红色字体

        方式3:抛出处理

        throws:写在方法定义处,表示声明一个异常,告诉调用者使用本方法可能会有哪些异常

public void 方法()  throws  异常类名1,异常类名2...{

                                        ...

                        }

         编译时异常:必须要写

         运行时异常:可以不写

         throw:写在方法内,结束方法。手动抛出异常对象,交给调用者,方法中下面的代码不再执行了

   public void 方法() {

           throw new NullPointerException();

                        }

/*
        throws:写在方法定义处,表示声明一个异常。告诉调用者,使用本方法可能会有哪些异常。
        throw :写在方法内,结束方法。手动抛出异常对象,交给调用者。方法中下面的代码不再执行了。
        需求:
            定义一个方法求数组的最大值
*/
        int[] arr = null;
        int max = 0;
        try {
            max = getMax(arr);
        } catch (NullPointerException e) {
            System.out.println("空指针异常");
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("索引越界异常");
        }
        //出现异常程序没有停止,继续执行
        System.out.println(max);
    }

    public static int getMax(int[] arr)/* throws NullPointerException,ArrayIndexOutOfBoundsException*/{
        if(arr == null){
            //手动创建一个异常对象,并把这个异常交给方法的调用者处理
            //此时方法就会结束,下面的代码不会再执行了
           throw new NullPointerException();
        }

        if(arr.length == 0){
            //手动创建一个异常对象,并把这个异常交给方法的调用者处理
            //此时方法就会结束,下面的代码不会再执行了
            throw new ArrayIndexOutOfBoundsException();
        }


        System.out.println("看看我执行了吗?");
        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if(arr[i] > max){
                max = arr[i];
            }
        }
        return max;
    }

        /*运行结果:
        * 空指针异常
          0
          * */

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梧桐小玉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值