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();
}
}
}
一个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();
}
}
}
相关文章推荐
- Ruby中的异常处理代码编写示例
- MySQL抛出Incorrect string value异常分析
- 详解JavaScript中的异常处理方法
- java程序中的延时加载异常及解决方案
- 解析Java异常的栈轨迹及其相关方法
- .NET(C#):Emit创建异常处理的方法
- windows7服务器上weblogic启动失败异常解决方法
- 有关ajax的error与后台的异常问题解决
- 深入探讨JAVA中的异常与错误处理
- GO语言异常处理机制panic和recover分析
- 浅谈JAVA 异常对于性能的影响
- C++ 异常处理 catch(...)介绍
- php中异常处理方法小结
- PHP中异常处理的一些方法整理
- 浅析Lua编程中的异常处理
- Perl中捕获警告信息、异常信息并写入日志详解
- 格式导致的Excel导入sql出现异常的解决方法
- Powershell小技巧之捕获脚本内部的异常
- Java多线程之多线程异常捕捉
- 详解Java异常处理中throw与throws关键字的用法区别