异常

一、什么是异常

程序在运行过程当中发生的问题

点击查看代码
public class Main1 {
public static void main(String[] args) {
System.out.println("发生异常前");
int i = 1 / 0 ;//这里发生异常
System.out.println("发生异常后");
	}
}

Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.qf.demo01.Main1.main(Main1.java:6)

结论:
异常会打断程序的进行

什么是异常

所以异常当然是指程序运行过程当中出现的问题,但是其本质,就是一个标识。

异常的结构和分类

Throwable:是所有异常和错误的共同父类
2.Error:错误是不可挽回的,不可修正的。(编译错误,类的缺失)
3.Exception:异常,是可以通过程序修正的。修正并不是把问题修改到没有,而是去进行策略上的弥
补。
a.Exception:受查异常(要么捕获,要么抛出
b.RuntimeException:运行时异常

异常的捕获

1.使用try,捕获某个区域当中,可能会发生的异常
2.catch用于匹配异常,如果异常不匹配,则try catch失效
3.多catch匹配,可以同时捕获多种异常
注意:在try当中只要发生异常,try代码块中无论还有多少代码没有执行,抵达对应的catch,执行catch的内容
4.在异常的catch列表当中,父类异常必须在后面

点击查看代码
package com.qf.demo01;
        public class Main2 {
            public static void main(String[] args) {
                System.out.println("发生异常前");
                try{String str = null;
                    str.length();
                    int i = 1 / 0 ;
                }catch (NullPointerException e){
                    e.printStackTrace();//打印异常的堆栈信息
                }catch (ArithmeticException e){
                    e.printStackTrace();//打印异常的堆栈信息
                }catch (Exception e){//父类异常必须在后面
                    e.printStackTrace();//打印异常的堆栈信息
                }
                System.out.println("发生异常后");
            }
        }

finally

无论如何都会被执行的代码块,即便return也无法组织。想要阻止他,只能强制关闭虚拟机。

点击查看代码
 public static void main(String[] args) {
                System.out.println("发生异常前");
                try{
                    String str = null;
                    str.length();
                    int i = 1 / 0 ;
                }catch (NullPointerException e){
                    e.printStackTrace();//打印异常的堆栈信息
                }catch (ArithmeticException e){
                    e.printStackTrace();//打印异常的堆栈信息
                }catch (Exception e){
                    e.printStackTrace();//打印异常的堆栈信息
                }finally {
                    System.out.println("无论如何都会执行");
                }
                System.out.println("发生异常后");
           }

自动抛出异常

checked异常通常可以考虑抛出
处理异常的策略:谁调用谁处理

点击查看代码
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class Main3 {
            public static void main(String[] args) {
                A a = new A();
                try {//捕获异常为最终处理异常的方式
                    a.fun();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
        class A{
            B b = new B();
            public void fun() throws Exception {
                b.fun();
            }
        }
        class B{
            C c = new C();
            public void fun() throws Exception {
                c.fun();
            }
        }
        class C{
            public void fun() throws ParseException , Exception {
                SimpleDateFormat sdf = new SimpleDateFormat("");
                sdf.parse("");
            }
        }

异常的堆栈信息

点击查看代码
~~~~Exception in thread "main" java.lang.RuntimeException: java.text.ParseException:
Unparseable date: ""
at com.qf.demo01.Main3.main(Main3.java:12)
Caused by: java.text.ParseException: Unparseable date: ""
at java.text.DateFormat.parse(DateFormat.java:366)
at com.qf.demo01.C.fun(Main3.java:31)
at com.qf.demo01.B.fun(Main3.java:25)
at com.qf.demo01.A.fun(Main3.java:19)
at com.qf.demo01.Main3.main(Main3.java:10)

打印堆栈信息:e.printStackTrace();
最下面的是最开始的调用方法,最上面的是最开始出现异常的地方。
异常的信息包含:
1.异常的内容
2.异常的类型
3.异常所发生的类
4.异常所发生的方法
5.异常发生的位置,即行数

手动抛出异常

由开发者自己定义的异常行为

点击查看代码
class Person{
    private Integer age;
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        if(age < 0 || age > 120){
            throw new RuntimeException("年龄错啦,您确定是人类吗?");
        }
        this.age = age;
    }
}
public class Main4 {
    public static void main(String[] args) {
        Person p = new Person();
        p.setAge(1111);
    }
}

结果

点击查看代码
Exception in thread "main" java.lang.RuntimeException: 年龄错啦,您确定是人类吗?
at com.qf.demo01.Person.setAge(Main4.java:18)
at com.qf.demo01.Main4.main(Main4.java:6)

自定义异常

自定义异常的创建方式:
1.继承Exception或者RuntimeException
2.名字通常是XxxxException
3.对父类构造进行调用

点击查看代码
public class AgeBoundException extends RuntimeException{
    public AgeBoundException() {
        super();}
    public AgeBoundException(String message) {
        super(message);
    }
    public AgeBoundException(String message, Throwable cause) {
        super(message, cause);
    }
    public AgeBoundException(Throwable cause) {
        super(cause);
    }
    protected AgeBoundException(String message, Throwable cause, boolean
            enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }
}
    public void setAge(Integer age) {
        if(age < 0 || age > 120){
            throw new AgeBoundException("年龄错啦,您确定是人类吗?");
        }
        this.age = age;
    }
posted @ 2023-03-25 19:58  卡卡罗特kk  阅读(77)  评论(0)    收藏  举报