Java之异常处理

本文详细介绍了Java中的异常处理机制,包括try-catch-finally的使用,throw和throws的区别,以及如何自定义异常。通过示例代码展示了异常的传递和处理流程,并探讨了静态修饰符static在类加载和初始化过程中的作用。
摘要由CSDN通过智能技术生成

Java之异常处理

1.异常处理

关键词作用备注
try异常发现区间执行代码逻辑,在执行期间发掘是否有异常
catch捕获异常后处理区间若try区间有异常,捕获异常在此区间处理
finally总是执行不论是否有异常 此区间代码总是执行 释放占用资源 如:IO流
throw抛出方法中异常异常扔到方法定义上(甩锅给上级)
throws抛出方法定义上异常异常扔到调用方法的地方(甩锅给上级)
package com.exception;
// 异常处理五个关键词
// try 异常发现区间
// catch 异常处理区间 若try区间有异常,捕获异常在此区间处理
// finally 不论是否有异常  此区间代码总是执行  释放占用资源 如:IO流
// throw 抛出异常  方法中  异常扔到方法定义上(甩锅给上级)
// throws 抛出异常  在方法定义上 异常扔到调用方法的地方(甩锅给上级)
public class Test01 {


    public static void main(String[] args) {
        // finally会阻止try catch 的return
//        System.out.println(new Test01().e2()); // 2
        System.out.println(new Test01().e3()); // 2

    }

    public void exceptions (int a,int b) throws ArithmeticException{//  此处异常 传递给方法调用第28行 new Test01().exceptions(1,0);
        if(b==0){
            throw new ArithmeticException();//  此处异常 传递给方法定义第18行  public void exceptions (int a,int b) throws ArithmeticException
        }else {
            System.out.println("666");
        }
    }

    public void e1(){
        try { // 异常发现区间
            new Test01().exceptions(1,0);
        } catch (ArithmeticException e) { // 捕获异常后处理区间
            e.printStackTrace();
            System.out.println("catch");
        }finally { // 总是执行
            System.out.println("始终执行");
        }
    }

    // 测试finally会阻止try catch 的return
    public int e2(){
        try {
            new Test01().exceptions(1,0);
            return 0;
        } catch (ArithmeticException e) {
            e.printStackTrace();
            System.out.println("catch");
            return 1;
        }finally {
            System.out.println("始终执行");
            return 2;
        }
    }


    // 测试finally会阻止try catch 的return
    public int e3(){
        try {
            new Test01().exceptions(1,1);
            return 0;
        } catch (ArithmeticException e) {
            e.printStackTrace();
            System.out.println("catch");
            return 1;
        }finally {
            System.out.println("始终执行");
            return 2;
        }
    }
}

2.自定义异常处理

MyException类为自定义的异常处理类,Test02类调用异常类
自定义异常类必须继承Exception类

MyException类

package com.exception;
// 自定义异常类必须继承Exception类
public class MyException extends Exception{
    private int num;

    // MyException构造方法
    public MyException(int num) {
        // 初始化num赋值
        this.num = num;
    }

    // 最终异常是什么
    @Override
    public String toString() {
        return "MyException"+this.num;
    }
}

Test02 类

package com.exception;

public class Test02 {
    static void e1(int a) throws MyException{  // 此处异常 传递给方法调用第18行  e1(10);
        System.out.println("传入参数:"+a);

        if(a<10){
            System.out.println("正常执行");
        }else {
            throw new MyException(a); //此处异常 传递给方法定义第4行 static void e1(int a) throws MyException{
        }

        System.out.println("结束方法");
    }

    public static void main(String[] args) {
        try {
            e1(10);
        } catch (MyException e) {
            System.out.println("可以添加逻辑处理");
            System.out.println(e); // 打印为MyException类中toString()方法
        }
        // 执行后打印
        // 传入参数:10
        // 可以添加逻辑处理
        // MyException10
    }
}

3.扩展之static

static为静态修饰符,与类一起加载。使用static修饰的方法或者属性,无需实例化, 可以使用类名直接调用。
探索父类 子类 初始化时 静态函数块 、 匿名函数 、 构造器的运行顺序
Person父类

package com.oop.demo06;

public class Person {
    private String name;
    static {
        System.out.println("父类静态函数块");
    }

    {
        System.out.println("父类匿名函数块");
    }

    public Person(){
        System.out.println("父类无参构造函数");
    }

    public Person(String name){
        this.name = name;
    }

}

Student类为子类

package com.oop.demo06;
// 静态导入类
import static java.lang.Math.random;

public class Student extends Person {
    // 程序运行时只执行一次
    static {
        System.out.println("子类静态函数块");
    }

    {
        System.out.println("子类匿名函数块");
    }

    public Student(){
        System.out.println("子类无参构造函数");
    }

    public static void main(String[] args) {
        Student student = new Student();

        // 静态导入类
        System.out.println(random());
    }
}
//    父类静态函数块
//    子类静态函数块
//    父类匿名函数块
//    父类无参构造函数
//    子类匿名函数块
//    子类无参构造函数

总目录,请点击此处,Java学习

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值