他底下有兩個子類:java.lang.Error與java.lang.Exception.咱們日常所說的異常,值的就是:java.lang.Exception.java
2法格式:數據庫
Try{數組 //編譯期間可能要出現異常的代碼服務器 }catch(異常類型 異常對象){網絡 /打印異常的代碼jvm /打印異常的信息學習 }this |
解析:try: 該代碼塊中編寫可能產生異常的代碼。spa
Catch: 用來進行某種異常的捕獲,實現對異常捕獲的處理,線程
Try和catch 不能單獨使用,必須結合使用。
public class Test0 { public static void main(String[] args) { int aa=10; int bb=0; try{ int cc=aa/bb; System.out.println(cc); }catch (ArithmeticException ce){ ce.printStackTrace(); } System.out.println("執行了嗎??");
} } |
經過以上程序咱們發現,catch 只能處理try中所對應的異常類型,若是發生的異常和catch不匹配。則是沒法處理的。所以try 後面能夠有多個catch出現,每一個catch處理不一樣的異常的類型。
public class Test0 { public static void main(String[] args) { int aa=10; int bb=0; int[]sum={1,2,3,4}; try{ int cc=aa/bb; //數組下標越界://java.lang.ArrayIndexOutOfBoundsException System.out.println(sum[4]); System.out.println(cc); }catch (ArithmeticException ce){ ce.printStackTrace(); }catch (ArrayIndexOutOfBoundsException ce){ ce.printStackTrace(); } System.out.println("執行了嗎??");
} } |
Catch執行完後,代碼的順序是繼續從Catch往下執行的,不會返回try語句塊中,因此try..catch 會幫咱們捕獲異常,可是會影響程序的正常的流程,最終咱們仍是要程序猿手動去處理髮生的異常。
try{ System.out.println(10/0); }catch (ArrayIndexOutOfBoundsException ce){ ce.printStackTrace(); }finally { System.out.println("無論上面的異常是否執行,finall永遠執行!!"); } } |
例如:
1.throw new ArithmeticException(「除數不能爲零」); 2.throw new ArrayIndexOutOfBoundsException(「數組下標越界」); |
5.
/throw 拋出異常 public class Test2 { public static void main(String[] args) { int[]num={1,2,3,4,5,6,7}; int index=10; getSting(num,index); } public static void getSting(int[]num,int index){ //判斷條件,若是知足,當執行完 throw 拋出異常對象後,方法已經沒法繼 續運行,這時就會結束當前方法的執行,並將其異常告知給調用者,這就是須要通 過異常來解決 if(index<0||index>num.length){ throw new ArrayIndexOutOfBoundsException("主人數組的下標太大的"); //System.out.println("ha"); //位於 throw new 下面的代碼不會被執行,報錯 }else{ System.out.println(num[index]); } } } //運行的結果:Exception in thread "main" // java.lang.ArrayIndexOutOfBoundsException: 主人數組的下標太大的 |
【注意】若是產生了異常,咱們就會將異常拋出,也就是將問題返回給該方法的調用者,那麼對於調用者來講,一種就是進行捕獲處理,另外一種就是繼續將異常拋出去,使用throw來聲明。
[訪問修飾符] 返回值類型 方法名(參數列表) throws 異常類名1….異常類2{ //代碼塊 } |
【注意】:throws只能用於方法的簽名的後面,而throw 只能用於方法體中。
練習題:要求:
咱們模擬登陸操做,若是用戶名已存在,則拋出異常並提示:該用戶名已被註冊。
a)首選自定義一個登陸異常類 LoginException
b)模擬登陸操做,使用數組或者 ArrayList 模擬數據庫中存儲的數據,而且提供當前註冊帳號是否存在的方法用於判斷。
//自定義一個異常類 public class LoginException extends Exception{ public LoginException() { }
public LoginException(String message) { super(message); }
public LoginException(String message, Throwable cause) { super(message, cause); }
public LoginException(Throwable cause) { super(cause); }
public LoginException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } }
public class Login { static String[]stuName={"liwenjie","zhangsan","李文傑","桑鳳嬌","郭朝旭"};
public static void main(String[] args){ Scanner input=new Scanner(System.in); System.out.println("請輸入用戶名"); String userName=input.next(); try{ boolean flag=display(userName); if(flag){ System.out.println("用戶註冊成功"); } }catch (Exception ce){ ce.printStackTrace(); } } public static boolean display(String name)throws Exception{ for(String ss:stuName){ if(ss.equals(name)){ //拋出一個異常 throw new LoginException("用戶名已被註冊過!!!"); } } return true; } }
//集合版的模擬登陸系統 public class Student{ public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.println("請輸入用戶名:"); String username=input.next(); try{ boolean flag=meth(username); if(flag){ System.out.println("註冊成功"); } }catch (Exception ce){ ce.printStackTrace(); } } public static boolean meth(String name)throws Exception{ ArrayList<String>list=new ArrayList<>(); list.add("sangfengjiao"); list.add("liwnejie"); list.add("mashitian"); list.add("趙雲"); list.add("zhangsan"); for(String ss:list){ if(ss.contains(name)){ throw new LoginException("用戶名已被註冊過!!!"); } } return true; } } |
2.6 3. 自定義異常類
每個學生(Student)都有學號,姓名和分數,分數永遠不能爲負數。若是老師給學生賦值一個負數,拋出一個自定義異常。
//自定義異常類 public class Student2 { private String stuName; private Integer stuXue; private Double stuScore; public Student2(){
} public Student2(String stuName,Integer stuXue,Double stuScore){ this.stuName=stuName; this.stuXue=stuXue; this.stuScore=stuScore; }
public String getStuName() { return stuName; }
public void setStuName(String stuName) { this.stuName = stuName; }
public Integer getStuXue() { return stuXue; }
public void setStuXue(Integer stuXue) { this.stuXue = stuXue; }
public Double getStuScore() { return stuScore; }
public void setStuScore(Double stuScore){ if(stuScore<0){ throw new StudentScore("分數不能爲負數!!!!"); }else{ this.stuScore = stuScore; }
} } public class StudentScore extends RuntimeException{ public StudentScore() { }
public StudentScore(String message) { super(message); }
public StudentScore(String message, Throwable cause) { super(message, cause); }
public StudentScore(Throwable cause) { super(cause); }
public StudentScore(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } } public class Stundent3 { public static void main(String[] args) { //建立Student對象 Student2 ss=new Student2(); ss.setStuName("李文傑"); //拋出:day02.StudentScore: 分數不能爲負數!!!! ss.setStuScore(-100.0); ss.setStuXue(33); System.out.println(ss.getStuName()); System.out.println(ss.getStuScore()); System.out.println(ss.getStuXue()); } } |