码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA中如何自定义异常

时间:2020-10-26 10:40:55      阅读:20      评论:0      收藏:0      [点我收藏+]

标签: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()<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());

        }
    }
}

JAVA中如何自定义异常

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

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!