zl程序教程

您现在的位置是:首页 >  后端

当前栏目

(JavaSE)认识异常

异常JavaSE 认识
2023-09-27 14:28:30 时间

⭐异常的分类

在这里插入图片描述

⭐异常的处理

在这里插入图片描述
先操作, 遇到问题再处理。后面代码可以执行

🎈try-catch捕获再处理

public static void main(String[] args) {
        try{
            System.out.println(10/0);
        }catch(ArithmeticException e){
            System.out.println("算数异常!");
        }
        System.out.println("执行后续代码!");
    }
算数异常!
执行后续代码!

如果没有去捕捉相应的异常,异常就会交给JVM处理,只要交给JVM处理程序就会立即终止。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
异常都是一个类
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

⭐异常的抛出

上述异常都是在程序运行/编译时抛出的,我们也可以主动抛出异常

🎈主动抛异常

🔥抛非受查异常

在这里插入图片描述

🔥抛受查异常

在这里插入图片描述
在这里插入图片描述
抛父类Exception默认是受查异常
在这里插入图片描述

public static void test22(int a) throws CloneNotSupportedException,ArithmeticException,NullPointerException {

可以声明受查或非受查异常,可以声明多个

⭐Finally

在这里插入图片描述
提示程序可以自动关闭scanner

try (Scanner scanner = new Scanner(System.in)) {//程序会自动关闭scanner
            int n = scanner.nextInt();
            System.out.println(n);
        } catch (InputMismatchException e) {
            e.printStackTrace();
            System.out.println("输入的数据不匹配");
        } finally {
            System.out.println("执行了finally部分,一般用来关闭资源!");
        }

finally部分一定会被执行

import java.util.InputMismatchException;
import java.util.Scanner;

public class TestFinally {
    public static int getData() {
        Scanner sc = null;
        try {
            sc = new Scanner(System.in);
            int data = sc.nextInt();
            return data;
        } catch (InputMismatchException e) {
            e.printStackTrace();
        } finally {
            return 100;
        }
    }
    public static void main(String[] args) {
        int data = getData();
        System.out.println(data);
    }
}
10
100

⭐自定义异常类

🎈自定义运行时异常

//LogIn.java
public class LogIn {
    private String userName = "admin";
    private String password = "123456";
    public  void loginInfo(String userName, String password) {
        try{
            if (!this.userName.equals(userName)) {
                throw new UserNameErrorException("用户名错误!");

            }
            if (!this.password.equals(password)) {
                throw new PassWordException("用户名错误!");
            }
            System.out.println("登陆成功");
        }catch(UserNameErrorException e){
            e.printStackTrace();
        }catch(PassWordException e){
            e.printStackTrace();
        }
}
public static void main(String[] args) {
    LogIn logIn=new LogIn();
    logIn.loginInfo("admin123", "123456");
  }
}
//UserNameErrorException.java
public class UserNameErrorException extends RuntimeException{
    public UserNameErrorException(){
        super();
    }
    public UserNameErrorException(String message){
        super(message);
    }
}
//PassWordException.java
public class PassWordException extends RuntimeException{
    public PassWordException(){
        super();
    }
    public PassWordException(String message){
        super(message);
    }
}

输出

UserNameErrorException: 用户名错误!
	at LogIn.loginInfo(LogIn.java:7)
	at LogIn.main(LogIn.java:26)

Process finished with exit code 0

🎈自定义编译时异常

public class UserNameErrorException extends Exception{
public class PassWordException extends Exception{

继承Exception类可以自定义编译时异常
在这里插入图片描述
处理办法:

public class LogIn {
    private String userName = "admin";
    private String password = "123456";
    public  void loginInfo(String userName, String password) throws UserNameErrorException, PassWordException {
        if (!this.userName.equals(userName)) {
            throw new UserNameErrorException("用户名错误!");
        }
        if (!this.password.equals(password)) {
            throw new PassWordException("用户名错误!");
        }
        System.out.println("登陆成功");

}
public static void main(String[] args) {

    try{
        LogIn logIn=new LogIn();
        logIn.loginInfo("admin123", "123456");
    }catch(UserNameErrorException e){
        e.printStackTrace();
    }catch(PassWordException e){
        e.printStackTrace();
    }
  }
}