在代码中,使用catch(Exceptione)的好处是()。
第1题:
classThreadExceptimplementsRunnable{publicvoidrun(){thrownewRuntimeException("exception");}publicstaticvoidmain(String[]args){newThread(newThreadExcept()).start();try{intx=Integer.parseInt(args[0]);Thread.sleep(x);System.out.print("main");}catch(Exceptione){}}}和命令行:javaThreadExcept1000哪一个是结果?()
A.main
B.编译失败
C.代码运行,但没有输出
D.mainjava.lang.RuntimeException:exception
第2题:
classOrderimplementsRunnable{publicvoidrun(){try{Thread.sleep(2000);}catch(Exceptione){}System.out.print("in");}publicstaticvoidmain(String[]args){Threadt=newThread(newOrder());t.start();System.out.print("pre");try{t.join();}catch(Exceptione){}System.out.print("post");}}可产生哪两项结果?()
A.inpre
B.prein
C.inprepost
D.preinpost
第3题:
如下代码:publicvoidTest(){try{oneMethod();System.out.println("condition1");}catch(ArrayIndexOutOfBoundsExceptione){System.out.println("condition2");}catch(Exceptione){System.out.println("condition3");}finally{System.out.println("finally");}}如果oneMethod正常运行,则输出结果中是?()
A.condition1finally
B.condition2finally
C.condition3finally
D.finally
第4题:
现有:voidtopGo(){try{middleGo();}catch(Exceptione){System.out.print("catch");}}voidmiddleGo()throwsException{go();system.out.print("latemiddle");}voidgo()throwsExceptiOn{thrownewException();}如果调用topGo(),则结果为:()
A.latemiddle
B.catch
C.latemiddlecatch
D.catchIatemiddle
第5题:
publicclassTestApp{publicstaticvoidmain(String[]args){try{inti=0;intj=1/i;Stringmyname=null;if(myname.length()>2)System.out.print(1”);}catch(NullPointerExceptione){System.out.print(2”);}catch(Exceptione){System.out.print(3”);}}}上述程序运行后的输出是哪项?()
A.3
B.2
C.231
D.32
第6题:
classWaitingimplementsRunnable{
booleanflag=false;
publicsynchronizedvoidrun(){
if(flag){
flag=false;
System.out.print("1");
try{this.wait();}catch(Exceptione){}
System.out.print("2");
}
else{
flag=true;
System.out.print("3");
try{Thread.sleep(2000);}catch(Exceptione){}
System.out.print("4");
notify();
}
}
publicstaticvoidmain(String[]args){
Waitingw=newWaiting();
newThread(w).start();
newThread(w).start();
}
}
以下哪两项是正确的?()
第7题:
classBirds{publicstaticvoidmain(String[]args){try{thrownewException();}catch(Exceptione){try{thrownewException();}catch(Exceptione2){System.out.print("inner");}System.out.print("middle");}System.out.print("outer");}}结果为:()
A.inner
B.innerouter
C.middleouter
D.innermiddleouter
第8题:
下列程序的输出结果是( )。 PublicclassTest{ Publicstaticvoidmain(String[]args){ int[]array=(2,4,6,8,10); intsize=6; intresult=-1; try{ for(inti=0;i<size&&result==-1;) if(array[i]==20)result=i; } catch(ArithmeticExceptione){ System.out.println("Catch---1"); } catch(Array IndexOutOfBoundsExceptione){ System.out.println("Catch---2"); } catch(Exceptione){ System.out.println("Catch---3"); } }
A.Catch---1
B.Catch---2
C.Catch---3
D.以上都不对
第9题:
现有:classBirds{publicstaticvoidmain(String[]args){try{thrownewException();}catch(Exceptione){try{thrownewException();}catch(Exceptione2){System.out.print("inner");}System.out.print("middle");}System.out.print("outer");}}结果是()
A.innerouter
B.middleouter
C.innermiddleouter
D..编译失败
第10题:
下列关于捕获异常的描述中,错误的是______。
A.在异常处理代码段中无法访问hy代码段中声明的变量
B.子类异常可被父类异常捕获处理
C.try-catch机制可以被用于流程控制
D.在catch代码段中的return语句用于退出方法,而不是返回异常抛出点