java自定义异常

一、

编写注册方法,判断注册的参数mobile是11位数字,否则抛出异常IllegalMobileException,调用者捕获异常并提示"手机不合法"。

SignupDemo.java:

 

public class SignupDemo {

	public static void main(String[] args) {

		String mobile = "12121121";

		String pass = "121212";

		try {

			boolean f = signup(mobile, pass);

		} catch (IllegalMobileException e) {

			System.out.println("手机不合法");

		}

	}



	public static boolean signup(String mobile, String pass) 

			throws IllegalMobileException {

		if (mobile.length() != 11) {

			throw new IllegalMobileException();

		}

		return false;

	}

}



IllegalMobileException.java:

 

 

 

 

public class IllegalMobileException extends Exception {

	public IllegalMobileException() {

		super();

	}



	public IllegalMobileException(String msg) {

		super(msg);

	}

}


二、学生登录,如果学号和密码不匹配,显示登录不成功,否则显示登录成功;另外,如果学号不是10位数字抛出自定义异常。IllegalStudentNumException继承运行时异常RuntimeException。

 

Student.java:

 

public class Student{

	public Student(){}

	public Student(String studentNo,String pass){

		this.studentNo = studentNo;

		this.pass = pass;

	}

	private String studentNo;

	private String pass;

	public String getStudentNo() {

		return studentNo;

	}

	public String getPass() {

		return pass;

	}

	public boolean equals(Object obj) {

		return super.equals(obj);

	}

}



SigninDemo.java:

 

 

 

 

public class SigninDemo {

	static List<Student> students=Arrays.asList(

			new Student("2015010101","1223"),

			new Student("2015010102","1234"),

			new Student("2015010112","2312")

			);



	public static void main(String[] args) {

		String mobile="12121121";

		String pass="121212";

		try {

			if(signin(mobile,pass)){

				System.out.println("成功登陆");

			}else{

				System.out.println("学号或密码不正确");

			}

		} catch (IllegalStudentNumException e) {

			System.out.println("学号格式不合法");

		}

	}

	

	public static boolean checkStuNo(String stuno) 

			throws IllegalStudentNumException{

		if (stuno.length() != 10) {

			throw new IllegalStudentNumException();

		}

		return true;

	}

	

	private static boolean signin(String stuno, String pass)

			throws IllegalStudentNumException{

		//判断stuno 为10位数字 不为十位数字抛出异常

		if(checkStuNo(stuno)){

			Student stu=new Student(stuno,pass);

			return students.contains(stu);

		}

		return false;

	}

}



IllegalStudentNumException.java:

 

 

 

 

public class IllegalStudentNumException extends RuntimeException{

	public IllegalStudentNumException(){

		super();

	}

	public IllegalStudentNumException(String msg){

		super(msg);

	}

}


 

三、在集合中存储着三个用户,分别为:"张三", "1234";"李四", "1232";"王五", "1231"。现实现控制台登录功能:

 

(1)密码正确:登录成功,任意键退出;

(2)用户名存在,密码错误:提示重新登录;

(3)用户名不存在,抛出自定义的NoUserFoundException,接收此异常后提示用户名不存在。

UserLogin.java:

 

public class UserLogin {

	// 初始化用户信息

	static UserInfo[] map = new UserInfo[3];

	static {

		map[0] = new UserInfo("张三", "1234");

		map[1] = new UserInfo("李四", "1232");

		map[2] = new UserInfo("王五", "1231");

	}



	public static void main(String[] args) {

		// main中捕获异常进行处理提示:用户名不存在

		boolean isExit = false;

		String username = "李四";

		String pass = "1232";

		try {

			isExit = login(username, pass);

			if (isExit) {

				System.out.println("登录成功");

			} else {

				System.out.println("密码错误");

			}

		} catch (NoUserFoundException e) {

			System.out.println("用户名不存在");

		}



	}



	public static boolean login(String username, String pass) throws NoUserFoundException {

		int i = containsUser(username);

		if (pass.equals(map[i].getPassword())) {

			return true;

		} else {

			return false;

		}

	}



	public static int containsUser(String username) throws NoUserFoundException {

		for (int i = 0; i < map.length; i++) {

			if (username.equals(map[i].getUsername())) {

				return i;

			}

		}

		throw new NoUserFoundException();

	}



}



UserInfo.java:

 

 

 

 

public class UserInfo {

	private String username;

	private String password;



	public UserInfo() {

	}



	public UserInfo(String username, String password) {

		this.username = username;

		this.password = password;

	}



	public String getUsername() {

		return username;

	}



	public void setUsername(String username) {

		this.username = username;

	}



	public String getPassword() {

		return password;

	}



	public void setPassword(String password) {

		this.password = password;

	}



	@Override

	public String toString() {

		return "username:" + this.getUsername() + ";password:"

				+ this.getPassword();

	}

}



NoUserFoundException.java:

 

 

 

 

public class NoUserFoundException extends Exception{

	public NoUserFoundException() {

		super();

	}

	public NoUserFoundException(String msg) {

		super(msg);

	}

}


 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

w_t_y_y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值