自定义异常
public class MyException extends Exception{ public MyException() { } public MyException(String m) { super(m); }
package com.primay.day5; public class Demo2 { public static void main(String[] args) { Demo1 d=new Demo1(); try { d.register("axb","123456"); } catch (MyException e) { System.out.println(e.getMessage()); } }
public class Demo1 { public void register(String name,String pswd) throws MyException{ if(null==name||name.length()<6||name.length()>14){ throw new MyException("用户名不合法"); } System.out.println("注册成功"); } }