今天稍微研究一下JAVA的异常处理机制
这种机制可以实现跳过出现异常的语句,例如下面这个程序:
package test5; public class Tech { public static void main(String[] args) { int i=82982; System.out.print(i/0); System.out.print(i); } }
我们可以看到,0不能做除数,所以运行时会报错:
Exception in thread "main" java.lang.Error: 无法解析的编译问题:
Scanner 无法解析为类型
Scanner 无法解析为类型
at test5.Tech.main(Tech.java:5)
但如果加上了如下的异常处理机制:
package test5; public class Tech { public static void main(String[] args) { int i=82982; try { System.out.print(i/0); }catch(ArithmeticException e) { } System.out.print(i); } }
就可以跳过这个愚蠢的除法继续运行程序,这是相当有意思的功能