(JavaSE)认识异常

⭐异常的分类

在这里插入图片描述

⭐异常的处理

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

🎈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();
    }
  }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值