Day 005_异常机制

异常机制

Exception

运行中出现的不期而至的各种状况

异常处理框架

java.lang.Throwable作为所有异常的超类

  1. Error
  2. Exception

Error

由Java虚拟机生成并抛出,大多数错误与代码编写者所执行的操作无关

Java虚拟机运行错误(Virtual MachineError)JVM一般会选择线程终止

致命性错误!不允许出现这样的情况

Exception

RuntimeException(运行时异常)一般由逻辑错误引起

异常处理五个关键字:try catch finally throw throws

try catch必须要用,可以不用finally

catch(想要捕获的异常类型)

catch可以写多个,捕获多个异常,最大的异常写在最下面,需要从小到大捕获

public class Test {
    
    
    public static void main(String[] args) {
    
    
        int a = 1;
        int b = 0;

        try{
    
    //try 监控区域
            System.out.println(a/b);
        }catch (ArithmeticException e){
    
    
            System.out.println("程序出现异常,变量b不能为0");
        }finally {
    
    
            System.out.println("finally");
        }
    }
}
public class Test {
    
    
    public static void main(String[] args) {
    
    
        int a = 1;
        int b = 0;

        try{
    
    //try 监控区域
            System.out.println(a/b);
        }catch (Error e){
    
    
            System.out.println("Error");
        } catch(Exception e){
    
    
            System.out.println("Exception");
        }catch (Throwable e){
    
    
            System.out.println("Throwable");
        }finally {
    
    
            System.out.println("finally");
        }
    }
    public void a(){
    
    
        b();
    }
    public void b(){
    
    
        a();
    }
}

结果:Exception
finally

快捷键:选中异常代码 ctrl+alt+t 选择try catch finall(MAC快捷键:option+command+t)

public class Test2 {
    
    
    public static void main(String[] args) {
    
    
        int a = 1;
        int b = 0;
        try {
    
    
            System.out.println(a/b);
        } catch (Exception e) {
    
     
            e.printStackTrace();//打印错误的栈信息  自动生成
        } finally {
    
    
        }
    }
}

throw主动抛出异常,一般用在方法中

public class Test {
    
    
    public static void main(String[] args) {
    
    
        new Test().test(1,0);
    }
    public void test(int a ,int b){
    
    
        if (b==0){
    
    
            throw new ArithmeticException();//主动抛出异常 throw
        }
        System.out.println(a/b);
    }
}

结果:

Exception in thread “main” java.lang.ArithmeticException
at exception.Test.test(Test.java:11)
at exception.Test.main(Test.java:7)

假设这方法中处理不了这个异常,方法上抛出异常

public class Test {
    
    
    public static void main(String[] args) {
    
    
        try {
    
    
            new Test().test(1,0);
        } catch (ArithmeticException e) {
    
    
            e.printStackTrace();
        }
    }
    public void test(int a ,int b) throws ArithmeticException{
    
    
        if (b==0){
    
    
            throw new ArithmeticException();//主动抛出异常 throw
        }
        System.out.println(a/b);
    }
}

自定义异常

用户自定义异常类,只需继承Exception类即可

步骤:

  1. 创建自定义异常类
  2. 在方法中通过throw关键字抛出异常对象
public class Test {
    
    
    static void test(int a) throws MyException {
    
    
        System.out.println("传递的参数为:" + a);
        if (a > 10) {
    
    
            throw new MyException(a);
        }
        System.out.println("OK");
    }

    public static void main(String[] args) {
    
    
        try {
    
    
            test(11);
        } catch (MyException e) {
    
    
            System.out.println("MyException=>"+e);
        }
    }
}

结果:

传递的参数为:11
MyException=>MyException{11}

  1. 处理运行时异常时,采用逻辑去合理规避同时辅助try-catch处理
  2. 在多重catch块后面,可以加上一个catch(Exception)来处理可能会被遗漏的异常
  3. 对于不确定的代码,也可以加上try-catch,处理潜在异常
  4. 尽量去处理异常,切忌只是简单地调用printStackTrace()去打印输出
  5. 尽量添加finally语句块去释放占用的资源

猜你喜欢

转载自blog.csdn.net/sssurprise/article/details/107424215