您的位置:首页 > 编程语言 > Java开发

java学习第九天之QQ登录验证 有关异常

2015-12-12 00:00 483 查看
异常

一个QQ登录验证 有关异常的小程序

类 LoginException

public class LoginException extends Exception{

private int type;

public int getType(){
return this.type;
}
public LoginException(String message, int type){
super(message);
this.type = type;
}
}
-----------

类 NameException

public class LoginException extends Exception{

private int type;

public int getType(){
return this.type;
}
public LoginException(String message, int type){
super(message);
this.type = type;
}
}
--------
类 PswdException

public class PswdException extends Exception{
public PswdException(String message){
super(message);
}
}
--------

类 Test

import com.qianfeng.demo2.MyException;

public class Test {
public String username = "admin";
public String password = "123";

public void login(String username, String password) throws NameException, PswdException, LoginException{
// 参数如果是null 值,那么很有可能存在java.lang.NullPointerException
/*if(username.equals("")){
System.out.println("进来了");
}*/

//字符串进行判断的时候,使用确认字符串跟未确认的字符串变量进行内容对比
/*if(username != null && !"".equals(username)){
System.out.println("进来了");
}else{
System.out.println("进不来了");
}*/

if(username == null || "".equals(username)){
throw new NameException("用户名不能为空");
}

if(password == null || "".equals(password)){
throw new PswdException("密码不能为空");
}

if("admin".equals(username) && "123".equals(password)){
System.out.println("登录成功.....");
}else{
throw new LoginException("账号或密码不正确", 3);
}
}

public void login2(String username, String password) throws Exception{

if(username == null || "".equals(username)){
throw new Exception("尊敬的客户,您的用户名不能为空");
}

if(password == null || "".equals(password)){
throw new Exception("尊敬的客户,密码不能为空");
}

if("admin".equals(username) && "123".equals(password)){
System.out.println("尊敬的客户,登录成功.....");
}else{
throw new Exception("尊敬的客户,账号或密码不正确");
}
}

public void login3(String username, String password) throws LoginException{

if(username == null || "".equals(username)){
throw new LoginException("尊敬的客户,您的用户名不能为空", 1);
}

if(password == null || "".equals(password)){
throw new LoginException("尊敬的客户,密码不能为空", 2);
}

if("admin".equals(username) && "123".equals(password)){
System.out.println("尊敬的客户,登录成功.....");
}else{
throw new LoginException("尊敬的客户,账号或密码不正确", 3);
}
}

public void divi(int a) throws MyException{
try{
int i = 10 / a;
System.out.println("计算成功");
}catch(Exception e){
//e.printStackTrace();
//重新抛一个异常,跟我们业务逻辑相关联的异常
//参数2 表示要保留原先系统出现原始错误,方便后期系统错误排查
throw new MyException("请确认您操作是否正确,如果问题依旧,"
+ "请联系客服.", e);
}
}

public void method(){

}

public static void main(String[] args) {

//自定义异常的第一种用法:
//异常运用:跟具体的业务有关,如果你的系统没有成型的异常处理系统,
//那么你不必须对原生系统异常包装
Test test = new Test();
/*try {
test.login("1", "1");
} catch (NameException e) {
e.printStackTrace();
} catch (PswdException e) {
e.printStackTrace();
} catch (LoginException e) {
e.printStackTrace();
}*/

/*try {
test.login2("1", "2");
} catch (Exception e) {
if("用户名不能为空".equals(e.getMessage())){
System.out.println("用户名不能为空");
}
if("密码不能为空".equals(e.getMessage())){
System.out.println("密码不能为空");
}
if("账号或密码不正确".equals(e.getMessage())){
System.out.println("账号或密码不正确");
}
}*/

/*try {
test.login3("1", "2");
} catch (LoginException e) {
switch (e.getType()) {
case 1:
System.out.println("用户名不能为空");
break;
case 2:
System.out.println("密码不能为空");
break;
case 3:
System.out.println("账号或密码不正确");
break;
default:
break;
}
//e.printStackTrace();
}*/

try {
test.divi(0);
} catch (MyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  异常