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()<6 || 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());
}
}
}