2023.7.31 周一:捕获和抛出异常 快捷键:Ctrl+Alt+D

发布时间 2023-07-31 15:30:55作者: 牟兆迪
import java.util.Scanner;

//try catch finally throw throws
public class Main {
    public static void main(String[] args) {
        int a = 1;
        int b = 0;
        //捕获多个异常要从小到大,finally部分可以删掉
        try {//监控区域
            if(b==0)
            {
                throw new ArithmeticException();//主动抛出异常
            }

            System.out.println(a / b);


        } catch (Error e) {//捕获异常
            System.out.println("Error");
        } catch(Exception e){
            System.out.println("Exception");
            e.printStackTrace();//打印错误的栈信息
        }catch(Throwable t){
            System.out.println("Throwable");
        } finally {//善后处理
            System.out.println("finally");
        }

    }
}