第10章 例外丛理 例外处理入门 受检丶执行时期例外 throw、 throws 例外的继承架构
第10章 • 例外处理 – 例外处理入门 – 受检、执行时期例外 – throw、throws – 例外的继承架构
例外处理入门 想尝试捕捉例外,可以使用"try"、" catch" " final!y"三个尖键词组合的语法来达到 try i //陈句 catch(例外型态名称){ //例外处理 finally i //一定会处理的区块
例外处理入门 • 想尝试捕捉例外,可以使用"try"、"catch"、 "finally"三个关键词组合的语法来达到 try { //陈述句 } catch(例外型态 名称) { //例外处理 } finally { //一定会处理的区块 }
例外处理入门 public class checkArgs Demo i public static void main(string[] args)[ try i System.out. printf("执行号s功能器n",args[01); catch (Array IndexOutofBoundsException e) System.out. println("没有指定自变量"); e printstackTrace()i
例外处理入门 public class CheckArgsDemo { public static void main(String[] args) { try { System.out.printf("执行%s功能%n", args[0]); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("没有指定自变量"); e.printStackTrace(); } } }
例外处理入门 例外处理最好只用于错误处理,而不应是 用于程序业务逻辑的一部份,因为例外的 产生要消耗瓷源
例外处理入门 • 例外处理最好只用于错误处理,而不应是 用于程序业务逻辑的一部份,因为例外的 产生要消耗资源
例外处理入 以下应用例外处理的方式就不适当 while(true) try t System. out. println(args[i])i +; catch(ArrayIndexOutofBounds Exception e)t 下面的方式才是正确的 for(int i=0; i< args. length i++) System. out. println(args[i])i
例外处理入门 • 以下应用例外处理的方式就不适当 • 下面的方式才是正确的 while(true) { try { System.out.println(args[i]); i++; } catch(ArrayIndexOutOfBoundsException e) { // .... } } for(int i = 0; i < args.length; i++) { System.out.println(args[i]); }