异常处理

1.什么是异常

软件在运行过程中,非常可能遇到异常问题,也就是异常(Exception)

2.简单分类

  1. 检查性异常:程序员无法预见的问题
  2. 运行时异常:编译时被忽略,但是时可能被程序员避免的
  3. 错误:错误不是异常,而是脱离程序员控制的问题。比如:栈溢出

3.异常体系结构

java把异常当对象处理,并定义一个类java.lang.Throwable作为所有异常的超类。

在java API中以及定义了许多异常类,分为:Error和Exception。

4.Error

  1. Error类对象由java虚拟机生成并抛出,大多数错误与代码编写者所执行的操作无关
  2. OutOfMemoryError,jvm资源不再由继续执行操作所需的资源
  3. 类定义错误等等

5.Exception

  1. 在Exception中有一个重要的子类RuntimeException(运行时异常)
  2. 这些异常一般由程序逻辑引起的,程序应该从逻辑角度尽可能避免这些异常的发生。
  3. Error和Exception的不同:Error通常是毁灭性的问题,Exception通常是可以被程序处理的,并且尽可能的处理这些异常。

6.异常处理机制

(1).捕获异常

try(){

}catch(){

}finally(){

}

捕获异常

(2).抛出异常

throw:主动抛出异常

throws:方法上抛出异常

(3).异常处理五个关键字

try,catch,finally,throw,throws

package oop.exception;

public class Test {
    public static void main(String[] args) {

        int a = 1;
        int b = 0;
        try {
            new Test().test(a,b);
        } catch (ArithmeticException e) {
            e.printStackTrace();
        }


        // 要捕获多个异常,要从小到大!
        try {   // try监控区域

        }catch (Error error){ // 捕获异常 catch(想要捕获的异常类型。Throwable最高)
            System.out.println("Error");
        }catch (Exception exception){
            System.out.println("Exception");
        }catch (Throwable throwable){
            System.out.println("throeable");
        }
        finally { // 处理善后工作
            System.out.println("finally");
        }


        // finally 可以不要 , 假设IO流,资源。关闭!

    }
    // 假设这方法中,处理不了这个异常。方法上抛出异常
    public void test(int a,int b) throws ArithmeticException{
        if(b == 0) { // 主动抛出异常 throw,throes
            throw new ArithmeticException(); // 主动抛出异常,一般在方法中使用
        }
    }
}
package oop.exception;

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

        // ctrl + alt + t
        try {
            System.out.println(a/b);
        } catch (Exception exception) {
            System.exit(0);
            exception.printStackTrace(); // 打印错误栈信息
        } finally {
        }
    }
}

7.自定义异常

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

步骤:

  1. 创建自定义异常类
  2. 在方法中通过throw关键词抛出异常
  3. 想在当前抛出异常的方法中处理异常,可以使用try-catch语句捕获并处理;否则在方法声明处通过throws关键字指明要抛出给方法调用者的异常,继续下一步操作。
  4. 再出现异常方法的调用者中捕获并处理异常。
package oop.exception;

// 自定义异常类
public class MyException extends Exception{

    // 传递数字>10;
    private int detail;

    public MyException(int detail) {
        this.detail = detail;
    }
    // toString

    @Override
    public String toString() {
        return "MyException{" +
                "detail=" + detail +
                '}';
    }
}

package oop.exception;

public class Testexception {
    // 可能会存在异常的方法

    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);
        }
    }

}

posted on 2022-03-30 15:57  新火拭茶  阅读(59)  评论(0)    收藏  举报