import java.util.ArrayList; import java.util.List; public class A{ private int[] arr=new int[3]; private List<String> ls; public A(){ ls=new ArrayList<String>(); ls.add("12"); } public int getArrValue(int i){ return arr[i]; } public String getListValue(int i){ return ls.get(i); } public static void main(String[] args){ try{ A a=new A(); int v1=a.getArrValue(0); String v2=a.getListValue(0); throw new Exception("自定义异常发生"); }catch(java.lang.NullPointerException e){ System.out.println("a未实例化或A.ls未实例化"); }catch(java.lang.IndexOutOfBoundsException e){ System.out.println("A.arr取值下标越界或A.ls取值下标越界"); }catch(Exception e){ e.printStackTrace(); } } }
爪哇国新游记之十----异常初相识
时间: 2024-10-05 04:58:46