1-1
所有异常都必须捕获。F
1-2
一个try语句可以有多个catch语句与之对应。T
1-3
异常也是一个对象。T
1-4
用户可以自定义自己的异常类。T
1-5
可以使用throw语句来抛出异常。T
1-6
可以使用throws语句来指明方法有异常抛出。T
1-7
当一个方法在运行过程中产生一个异常,则这个方法会终止,但是整个程序不一定终止运行。T
1-8
程序运行时所产生的系统定义的异常将自动被抛出。T
1-9
Java程序执行时出现异常,也不一定是程序本身的错。T
1-10
有时Java程序执行过程中出现异常并不是程序员的错误造成的,例如网络不通,这时程序员在进行程序设计时无需处理这样的异常。F
2-1
假设方法unsafe() 将抛出IOException, 可以填入如下代码段第1行的选项是( )。
1)
2) { if(unsafe()){//do something…}
3) else if(safe()){//do the other…}
4) }
(2分)
A. public IOException methodName()
B. public void methodName()
C. public void methodName() throw IOException
D.public void methodName() throws IOException
2-2
下列哪种异常是检查型异常,需要在编写程序时声明 ( ).
(2分)
A. NullPointerException
B. ClassCastException
C. FileNotFoundException
D. IndexOutOfBoundsException
2-3
Which of the following could possibly cause a runtime error? 分值为2分。
(2分)
A. misspelled keyword
B. missing semicolon
C. divide by zero
D. incorrect class modifier
2-4
Suppose there is no file Hello.txt in the current directory. Run the program:
import java.io.*;
public class ABC {
public static void main(String argv[]) throws Exception {
ABC m=new ABC();
System.out.println(m.ff());
}
public int ff() {
try {
FileInputStream dis=new FileInputStream("Hello.txt");
} catch (FileNotFoundException fne) {
System.out.print("No such file found, ");
throw fne;
} finally {
System.out.print("Doing finally, ");
}
return 0;
}
}
(2分)
A. No such file found,
B. No such file found ,0
C. No such file found, Doing finally,
D. No such file found, Doing finally, 0
2-5
以下对异常的描述不正确的有
(1分)
A. 异常分为Error和Exception
B. Throwable是所有异常类的父类
C. Exception是所有异常类父类
D. Exception包括RuntimeException和RuntimeException之外的异常
2-6
在try-catch-finally语句块中,以下可以单独与finally一起使用的是
(1分)
A. catch
B. try
C. throws
D. throw
2-7
下面代码运行结果是
public class Demo{
public int add(int a,int b){
try{
return a+b;
}catch(Exception e){
System.out.println(“catch 语句块”);
}finally{
System.out.println(“finally 语句块”);
}
return 0;
}
public static void main(String[] args){
Demo demo = new Demo();
System.out.println(“和是:”+demo.add(9,34));
}
}
(2分)
A. 编译异常
B. finally语句块 和是:43
C. 和是:43 finally语句块
D. catch语句块 和是:43
2-8
以下描述不正确的有
(1分)
A. try块不可以省略
B. 可以使用多重catch块
C. finally块可以省略
D. catch块和finally块可以同时省略
2-9
以下对自定义异常描述正确的是
(1分)
A. 自定义异常必须继承Exception
B. 自定义异常可以继承自Error
C. 自定义异常可以更加明确定位异常出错的位置和给出详细出错信息
D. 程序中已经提供了丰富的异常类,使用自定义异常没有意义
2-10
以下程序运行结果是
public class Test {
public int div(int a, int b) {
try {
return a / b;
}catch(Exception e){
System.out.println(“Exception”);
}catch(NullPointerException e){
System.out.println(“ArithmeticException”);
}
catch (ArithmeticException e) {
System.out.println(“ArithmeticException”);
} finally {
System.out.println(“finally”);
}
return 0;
}
public static void main(String[] args) {
Test demo = new Test();
System.out.println(“商是:” + demo.div(9, 0));
}
}
(2分)
A. Exception finally 商是:0
B. ArithmeticException finally 商是:0
C. finally商是:0
D. 编译报错
2-11
对以下程序进行编译、运行结果是
abstract class MineBase {
abstract void amethod();
static int i;
}
public class Mine extends MineBase{
public static void main(String argv[]){
int[] ar = new int[5];
for(i = 0;i < ar.length;i++)
System.out.println(ar[i]);
}
}
(2分)
A. 打印5个0。
B. 编译出错,数组ar[]必须初始化。
C. 编译出错。
D. 出现IndexOutOfBoundes的异常
2-12
下面关于try块的说法正确的是?
(2分)
A. try块后至少应有一个catch 块
B. try块后必须有finally块
C. 可能抛出异常的语句应放在try块中
D. 对抛出的异常的处理应放在try块中
4-1
The output of the code below is:
public class Main {
static int count = 0;
public static void main(String[] args) {
for ( ;; ) {
try {
if ( count++ == 0 )
throw new Exception();
System.out.print("A");
} catch (Exception e) {
System.out.print("B");
} finally {
System.out.print("C");
if ( count == 2 )
break;
}}}}
BCAC
4-2
The output of the code below is:
class Test {
public static void main(String[] args) {
try {
System.out.println("Welcome to Java");
int i = 0;
int y = 2 / i;
System.out.println("Welcome to HTML");
}
finally {
System.out.println("The finally clause is executed");
}
System.out.println("End of the block");
}}
Welcome to Java
The finally clause is executed
4-3
请写出以下程序运行结果:
public class X {
public static void main(String [] args) {
try {
badMethod();
System.out.print("A");
} catch (RuntimeException ex) {
System.out.print("B");
} catch (Exception ex1) {
System.out.print("C");
} finally {
System.out.print("D");
}
System.out.print("E");
}
public static void badMethod() {
throw new RuntimeException();
}}
BDE
4-4
请写出以下程序运行结果:
class NoWater extends Exception {}
class NoDrinkableWater extends NoWater {}
public class FinallyWorks {
static int count = 0;
public static void main(String[] args) throws NoWater {
while ( true ) {
try {
count++;
if ( count == 1 ) {
System.out.println("OK");
} else if ( count == 2 ) {
System.out.println("Exception raised: NoDrinkableWater");
throw new NoDrinkableWater();
} else if ( count == 3 ) {
System.out.println("Exception raised: NoWater");
throw new NoWater();
}
} catch (NoDrinkableWater e) {
System.out.println(e);
} finally {
System.out.println("finally");
if ( count == 3 )
break;
}
}
}
}
OK
finally
Exception raised: NoDrinkableWater
NoDrinkableWater
finally
Exception raised: NoWater
finally
本文介绍了Java异常处理的基础知识,包括异常的捕获、处理和类型。讨论了try-catch-finally语句块的使用,以及如何自定义异常。同时,通过一系列的选择题和程序示例,解释了检查型异常与运行时异常的区别,强调了异常处理在程序健壮性中的重要性。

370

被折叠的 条评论
为什么被折叠?



