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

异常

一个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();

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值