Java异常:Java运行期出现的错误
观察错误的名字和行号最重要
package com.nyist; public class TextEx { public static void main(String[] args) { int [] arr = {1,2,3}; System.out.println(arr[3]); } }
异常:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at com.nyist.TextEx.main(TextEx.java:6)
示例:
package com.nyist; public class TextEx { public static void main(String[] args) { int [] arr = {1,2,3}; try { System.out.println(arr[3]); }catch(ArrayIndexOutOfBoundsException arre) { System.out.println("系统出错,请联系管理员:ldw_123456@163.com"); } System.out.println(arr[3]); } }
运行结果:
系统出错,请联系管理员:ldw_123456@163.com Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at com.nyist.TextEx.main(TextEx.java:11)
Error:系统错误,无法处理也无需处理
Exception:可以被处理的错误,可以被catch,
RuntimeException:运行时错误
原文:https://www.cnblogs.com/nyist0/p/12459427.html