Java 实例 - 获取异常的堆栈信息


以下实例演示了使用异常类的 printStack() 方法来获取堆栈信息:

  1. /*
  2. author by shouce.ren
  3. Main.java
  4. */
  5.  
  6. public class Main{
  7. public static void main (String args[]){
  8. int array[]={20,20,40};
  9. int num1=15,num2=10;
  10. int result=10;
  11. try{
  12. result = num1/num2;
  13. System.out.println("The result is" +result);
  14. for(int i =5;i *gt;=0; i--) {
  15. System.out.println("The value of array is"
  16. +array[i]);
  17. }
  18. }
  19. catch (Exception e) {
  20. e.printStackTrace();
  21. }
  22. }
  23. }

以上代码运行输出结果为:

  1. The result is1
  2. java.lang.ArrayIndexOutOfBoundsException: 5
  3. at testapp.Main.main(Main.java:28)