異常java
一個QQ登陸驗證 有關異常的小程序小程序
類 LoginExceptionthis
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;
}
}
-----------字符串
類 NameExceptionget
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;
}
}
--------
類 PswdExceptionit
public class PswdException extends Exception{
public PswdException(String message){
super(message);
}
}
--------io
類 Testclass
import com.qianfeng.demo2.MyException;test
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();
}
}
}import