分析以下代码,下面________ 陈述最准确?public class Test { public static void main(String[] args) { try { int zero = 0; int y = 2/zero; try { String s = "5.6"; Integer.parseInt(s); // Cause a NumberFormatException } catch(Exception e) { } } catch(RuntimeException e) { System.out.println(e); } }}
A: 一个try-catch块不能嵌入另一个try-catch块中。
B: 好的编程习惯是避免嵌套try-catch块,因为嵌套会使程序难以阅读。 最好只能使用一个try-catch块重写程序。
C: 程序出现编译错误,因为Exception出现在RuntimeException之前。
D: 其它都不对
A: 一个try-catch块不能嵌入另一个try-catch块中。
B: 好的编程习惯是避免嵌套try-catch块,因为嵌套会使程序难以阅读。 最好只能使用一个try-catch块重写程序。
C: 程序出现编译错误,因为Exception出现在RuntimeException之前。
D: 其它都不对
举一反三
- 以下程序有什么错误?public class Test {public static void main (String[] args) {try {System.out.println("Welcome to Java");}}} A: 没有catch块就不能有try块 B: 没有catch块或finally块就不能有try块 C: 未声明异常的方法调用,不能放在try块内 D: 没有错误
- 下面程序的执行结果是( )。public class Test {public static void main(String[] args) {new Test().test();}public void test(){try{System.out.print("try");}catch(ArrayIndexOutOfBoundsException e){System.out.print("catch1");}catch(Exception e){System.out.print("catch2");}finally{System.out.println("finally");}}} A: try finally B: try catch1 finally C: try catch2 finally D: finally
- (9-2)下面程序能够通过编译检查。 public class Demo5 { public static void main(String[] args) { try { System.out.println(3/0); System.out.println("你好"); }catch(Exception e) { e.printStackTrace(); }catch(ArithmeticException e) { e.printStackTrace(); } } }
- 下列程序的输出结果是______。 public class Test public static void main(String[]args) int[]array=(2,4,6,8,10); int size=6; int result=-1; try for(int i=0;i<size&&result==-1;) if(array[i]==20)result=i; catch(ArithmeticException e)﹛ System.out.println("Catch———1"); catch(ArrayIndexOutOfBoundsException e) System.OUt.println("Catch———2"); catch(Exception e) System.out.println("Catch———3");[/i] A: Catch———1 B: Catch———2 C: Catch———3 D: 以上都不对
- try { try块代码} catch (IOException e) { 异常处理代码1} catch (Exception e) { 异常处理代码2}以上try-catch结构的try块代码抛出异常IOException时,将执行异常处理代码1,异常处理代码2不被执行。( )