JAVA中如何自定义异常

2021-03-22 22:25

阅读:533

标签:nbsp   cep   sys   xtend   bsp   如何   exce   end   port   

public class MyExcption extends Exception{
    public MyExcption(){
    }
    public MyExcption(String s){
        super(s);
    }
}

ExcptionUser类
public class ExcptionUser {
   public void login(String name,String pwd) throws MyExcption {
       if(pwd==null || pwd.length()14){
            throw new MyExcption("密码不合法");
       }
        System.out.println("欢迎登陆");
   }
}
main类
import java.util.Scanner;
public class tesxtExption {
    public static void main(String[] args) {
        ExcptionUser u= new ExcptionUser();
        Scanner scan=new Scanner(System.in);
        System.out.println("请输入姓名");
        String name=scan.nextLine();
        System.out.println("请输入密码");
        String pwd=scan.nextLine();
        try {
            u.login(name,pwd);
        } catch (MyExcption e) {
           System.out.println(e.getMessage());

        }
    }
}

JAVA中如何自定义异常

标签:nbsp   cep   sys   xtend   bsp   如何   exce   end   port   

原文地址:https://www.cnblogs.com/baijianming/p/13870376.html


评论


亲,登录后才可以留言!