java学习之异常

java学习之异常

finally:

  1. 特点:被它控制的语句一定会被执行,特殊情况:在执行finally之前jvm退出了

示例:

package com.xu;

public class Test {
    public static void main(String[] args) throws Exception {
        try {
            System.out.println(10 / 0);
        } catch (Exception e) {
            System.out.println("除数不能为0");
        } finally {
            System.out.println("我被执行了");
        }
    }
}

示例2:只要执行了try 就会执行finally,把除数改成2

package com.xu;

public class Test {
    public static void main(String[] args) throws Exception {
        // 只要执行了try 就会执行finally
        try {
            System.out.println(10 / 2);
        } catch (Exception e) {
            System.out.println("除数不能为0");
        } finally {
            System.out.println("我被执行了");
        }
    }
}

示例3(特殊情况,在执行finally之前jvm退出了):

package com.xu;

public class Test {
    public static void main(String[] args) throws Exception {
        // 只要执行了try 就会执行finally
        try {
            System.out.println(10 / 2);
            System.exit(0);
        } catch (Exception e) {
            System.out.println("除数不能为0");
        } finally {
            System.out.println("我被执行了");
        }
    }
}

示例4;用return ,jvm并没有结束,所以还是会执行finally

package com.xu;

public class Test {
    public static void main(String[] args) throws Exception {
        // 只要执行了try 就会执行finally
        try {
            System.out.println(10 / 2);
            // System.exit(0);
            return;
        } catch (Exception e) {
            System.out.println("除数不能为0");

        } finally {
            System.out.println("我被执行了");
        }
    }
}

2.finally的作用:用来释放资源,在IO 操作中经常见到。

  • 面试题:下面程序运行后的输出结果是什么?
package com.xu;

public class Test {
    static int test() {
        int a = 10;
        try {
            a = 20;
            System.out.println(a / 0);
            return a;
        } catch (Exception e) {
            a = 30;
            return a;
        } finally {
            a = 40;
        }
    }

    public static void main(String[] args) throws Exception {
        System.out.println(test());
    }
}

答案:30,解析:a除以0会异常,直接跳到catch里,返回结果是30,然后执行finally,但是里面虽然赋值了,但并有有 return a 所以值还是30。

自定义异常

  1. 为什么要自定义异常?
    在这里插入图片描述

自定义运行时异常:
分为两步,1.首先要定义一个类,继承RuntimeException,2.抛出异常时,抛出自己定义 的异常类名。

示例:

package com.xu;

// 1.定义一个类,继承RuntimeException
class MyException extends RuntimeException {

}

public class Test {
    public static void main(String[] args) throws Exception {
        // 2.抛出异常时,抛出自己定义 的异常类名。
        throw new MyException();
    }
}

自定义编译时异常
示例:

package com.xu;

// 1.定义一个类,继承RuntimeException
class MyException extends Exception {

}

public class Test {
    public static void main(String[] args) throws MyException {
        // 2.抛出异常时,抛出自己定义 的异常类名。
        throw new MyException();
    }
}

自定义一个带参数异常
1.定义一个类继承Exception或RunTimeException
1.1.在类当中提供一个有参数的构造方法,
1.2.把参数传给父类构造器。
2.抛出异常时,抛出自己定义的异常类名,并且传入对应的参数。
示例:

package com.xu;
//1.定义一个类继承Exception或RunTimeException
class MyArgsException extends Exception {
    //1.1.在类当中提供一个有参数的构造方法,
    MyArgsException(String massage) {
        //1.2.把参数传给父类构造器。
        super(massage);
    }
}

public class Test {
    public static void main(String[] args) throws MyArgsException {
        // 2.抛出异常时,抛出自己定义的异常类名,并且传入对应的参数。
        throw new MyArgsException("卧槽,异常了!");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值