若有以下程序: class Demo{ public static void main(String []s){ int a = 5, b = 0; try{a = a/b;} catch(Exception e){System.out.print(“异常”)} catch(AirthmeticException e){ System.out.print(“算术异常”); } Catch(IndexOutOfBoundsException e){ System.out.print(“越界异常”); } }} 则程序运行结果为( )
A: 显示:异常
B: 显示:算术异常
C: 显示:越界异常
D: 编译错误
A: 显示:异常
B: 显示:算术异常
C: 显示:越界异常
D: 编译错误
举一反三
- (9-4)请根据提示补全程序空白处,使程序能够正确运行。 public class Demo { static void show() throws ① { System.out.println("抛出异常!"); throw new IllegalAccessException("一个异常"); } public static void main(String []args) { try { show(); }catch( ② e ) { e.printStackTrace(); } } }
- class MyException extends (1) { } public class Demo { public static void main(String[] args) { try { show(); } catch ( (2) e) { e.printStackTrace(); } } public static void show() (3) MyException { throw new MyException(); } 以上程序,创建了一个自定义异常(编译异常),请补全空白处代码
- 读程序,写出和程序输出格式一致的输出结果。 public class J_Test { public static void mb_method(int i) { try { if(i == 1) throw new Exception(); System.out.print("1"); } catch(Exception ex) { System.out.print("2"); return; } finally { System.out.print("3"); } System.out.print("4"); } public static void main(String[] args) { mb_method(0); mb_method(1); } }
- 下面程序的执行结果是( )。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-3)请阅读程序,然后写出程序运行结果。(请注意输出不换行) public class Demo3 { public static void main(String[] args) { int a=9,b=0; double x=9,y=0; try { int c=a/b; }catch(Exception e1) { System.out.print("1"); try { double z=x/y; }catch(Exception e2) { System.out.print("2"); }finally { System.out.print("3"); } }finally { System.out.println("4"); } } }