JAVA中如何自定义异常
2021-03-22 22:25
标签:nbsp cep sys xtend bsp 如何 exce end port public class MyExcption extends Exception{ ExcptionUser类 } JAVA中如何自定义异常 标签:nbsp cep sys xtend bsp 如何 exce end port 原文地址:https://www.cnblogs.com/baijianming/p/13870376.html
public MyExcption(){
}
public MyExcption(String s){
super(s);
}
}
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());
}
}