TestException

来源:互联网 发布:怎么重置mac 编辑:程序博客网 时间:2024/06/05 06:03
public class TestException {
    public static void main(String[] args) {
        try {
            System.out.println(sum(new int[] {1, 2, 3, 4, 5}));
        }
        catch(Exception ex) {
            ex.printStackTrace();
            System.out.println("\n" + ex.getMessage());
            System.out.println(ex.toString());
           
            StackTraceElement[] element = ex.getStackTrace();
            System.out.println("Trace Info Obtained from getStackTrace");
            for(int i = 0; i < element.length; i++) {
                System.out.print("Method " + element[i].getMethodName());
                System.out.print(" (" + element[i].getClassName() + ": ");
                System.out.println(element[i].getLineNumber() + ")");
            }
        }
    }
   
    private static int sum(int[] list) {
        int result = 0;
        for(int i = 0; i <=list.length; i++)
            result += list[i];
        return result;
    }
}






java.lang.ArrayIndexOutOfBoundsException: 5
    at TestException.sum(TestException.java:24)
    at TestException.main(TestException.java:4)

5
java.lang.ArrayIndexOutOfBoundsException: 5
Trace Info Obtained from getStackTrace
Method sum (TestException: 24)
Method main (TestException: 4)



0 0
原创粉丝点击