天天看點

在方法中抛出異常

學習内容:

1.使用throws關鍵字抛出異常

代碼示例:

public class Shoot {
 static void pop()throws NegativeArraySizeException{//定義方法
    int[]arr=new int[-3];//建立數組
  }
  public static void main(String[] args) {
    try {
      pop();
    }catch(NegativeArraySizeException e){
      System.out.println("pop()方法異常");//輸出異常資訊
    } }
}      

運作截圖:

在方法中抛出異常

 明天任務:I/O