How to customize the exception class

First define an exception class

	package Day20;/*
 *@author wanghongyuan
 *@Create 2020/12/31 01:09
 */
/*
    自定义异常类
        java提供的异常类,不够我们使用,需要我们自己定义一些异常类
    格式:
        public class XXXXException extends Exception 或者RuntimeException{
                添加一个空参构造方法
                添加一个带异常信息的构造方法
        }
     注意:
        1.自定义异常类一般都是以Exception结尾,说明该类是个异常类
        2.自定义异常类,必须继承Exception 或者RuntimeException
            继承Exception的话:那么自定义的异常类就是一个编译期异常,如果方法内部抛出了编译器异常,就必须处理这个异常,要么Try()。。。。catch或者throws
            继承RuntimeException的话:那么这个自定义的异常类就是一个运行期异常,无需处理,直接交给虚拟机处理(中断处理)
 */
public class RegisterException extends RuntimeException {
    
    
    // 添加一个空参的构造方法
    public RegisterException() {
    
    
    }
    // 添加一个带异常信息的构造方法
    /*
        查看源码发现,所有异常类都会有个带异常信息的构造方法,让父类来处理这个异常信息。
     */
    public RegisterException(String message) {
    
    
        super(message);
    }
}

Try writing a demo, this demo is an exception class inheriting Exception.

package Day20;/*
 *@author wanghongyuan
 *@Create 2020/12/31 00:06
 */

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/*
    要求:我们模拟注册操作,如果用户名已存在,则抛出异常并提示:亲,该用户名已被注册。

    分析:
        1.使用数组保存已经注册过的用户名(数据库存)
        2.使用Scanner获取用户输入的注册的用户名(前端,页面)
        3.定义一个方法,对用户输入的用户名进行判断
            遍历存储已经注册过的用户名,获取每一个用户名
            使用获取到的用户名和用户输入的用户比较
            true:
                用户名已经存在,抛出RegisterException异常,告知用户:亲,该用户名已被注册。
            false:
                继续遍历比较
           如果循环结束了,还没有找到重复的用户名,提示用户:“恭喜你注册成功!”

  */
public class Demo01RegisterException {
    
    
    static String[] usernames={
    
    "张三","李四","王五","马六"};
    public static void main(String[] args) {
    
    
         Scanner sc = new Scanner(System.in);
         System.out.println("请输入用户名:");
         String username = sc.next();
         checkUsername(username);
     }
    // 3定义一个方法,对用户输入注册用户名进行判断
    public static void checkUsername(String username){
    
    
         // 遍历存储已经注册过用户名的数组,获取每一个用户名
        for (String s : usernames) {
    
    
            if (s.equals(username)){
    
    
                // true
                try {
    
    
                    throw new RegisterException("亲,该用户名已被注册。");
                } catch (RegisterException e) {
    
    
                    e.printStackTrace();
                    return;// 结束方法
                }
            }
        }
        // 遍历完了都没有找到匹配的,就提示用户成功注册。
        System.out.println("恭喜你注册成功");

    }
}

Try writing a demo, this demo is an exception class inheriting RuntimeException.

package Day20;/*
 *@author wanghongyuan
 *@Create 2020/12/31 00:06
 */

import java.util.Scanner;

/*
    要求:我们模拟注册操作,如果用户名已存在,则抛出异常并提示:亲,该用户名已被注册。

    分析:
        1.使用数组保存已经注册过的用户名(数据库存)
        2.使用Scanner获取用户输入的注册的用户名(前端,页面)
        3.定义一个方法,对用户输入的用户名进行判断
            遍历存储已经注册过的用户名,获取每一个用户名
            使用获取到的用户名和用户输入的用户比较
            true:
                用户名已经存在,抛出RegisterException异常,告知用户:亲,该用户名已被注册。
            false:
                继续遍历比较
           如果循环结束了,还没有找到重复的用户名,提示用户:“恭喜你注册成功!”

  */
public class Demo02RegisterException {
    
    
    static String[] usernames={
    
    "张三","李四","王五","马六"};
    public static void main(String[] args) /*throws RegisterException*/ {
    
    
         Scanner sc = new Scanner(System.in);
         System.out.println("请输入用户名:");
         String username = sc.next();
         checkUsername(username);
     }
    // 3定义一个方法,对用户输入注册用户名进行判断
    public static void checkUsername(String username) /*throws RegisterException*/ {
    
    
         // 遍历存储已经注册过用户名的数组,获取每一个用户名
        for (String s : usernames) {
    
    
            if (s.equals(username)){
    
    
                // true
                    throw new RegisterException("亲,该用户名已被注册。");// 抛出运行期异常,无需处理,交给JVM处理,中断处理
            }
        }
        // 遍历完了都没有找到匹配的,就提示用户成功注册。
        System.out.println("恭喜你注册成功");

    }
}

Comparing these two demos, it is found that if the inheritance is RuntimeException, then there is no need to deal with the runtime exception. JVM will handle
it. If the inheritance is Exception, then there will be a compile-time exception, which needs to be handled by yourself. Use try or throws.

Guess you like

Origin blog.csdn.net/weixin_41977380/article/details/112001674