Discuss / Java / 打卡

打卡

Topic source

蛤蟆猪哥

#1 Created at ... [Delete] [Delete and Lock User]

public class CustomExceptionDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			String token = login("admin","111112");	
			System.out.println(token);
		}catch(UserNotFoundException e) {
			System.out.println("User Not Found!");
		}catch (PasswordErrorException e) {
			System.out.println("Password Error!");
		}
	}
	
	static String login(String username, String password) {
		if(username.equals("admin")) {
			if(password.equals("111111")) {
				return username+" login successed.";
			}else {
				throw new PasswordErrorException("Password Error!");
			}
		}else {
			throw new UserNotFoundException("User Not Found!");
		}	
	}
}

class UserNotFoundException extends BaseException{
	public UserNotFoundException() {
		super();
	}
	
	public UserNotFoundException(String message) {
		super(message);
	}
}

class PasswordErrorException extends BaseException{
	public PasswordErrorException() {
		super();
	}
	
	public PasswordErrorException(String message) {
		super(message);
	}
}


  • 1

Reply