第十二章单元测试
- 关于异常,下列说法正确的是( )。
- java中用来抛出异常的关键字是( )。
- 当方法遇到异常又不知如何处理时,下列哪种说法是正确的( )。
- ( )类是所有异常类的父类。
- 下列哪种异常是检查型异常,需要在编写程序时声明( )。
以下关于异常的代码的执行结果是( )。
public class Test {
public static void main(String args[]) {
try {
System.out.print("try");
return;
} catch(Exception e){
System.out.print("catch");
}finally {
System.out.print("finally");
}
}
}- 在异常处理中,如释放资源、关闭文件等由( )来完成。
阅读如下Java代码,在控制台输入"-1",执行结果是( )。
import java.util.InputMismatchException;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入数字:");
try {
int num = input.nextInt();
if (num < 1 || num > 4) {
throw new Exception("必须在1-4之间!");
}
} catch (InputMismatchException e) {
System.out.println("InputMismatchException");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}- 数组下标越界,则发生异常,提示为( )。
- 对于catch子句的排列,下列哪种是正确的( )。
A:异常是一种对象。 B:异常就是错误。 C:为了保证程序运行速度,要尽量避免异常。 D:一旦程序运行,异常将被创建 。
答案:异常是一种对象。
A:catch B:finally C:try D:throw
A:声明异常 B:嵌套异常 C:抛出异常 D:捕获异常
A:Error B:Throwable C:Exception D:AWTError
A:FileNotFoundException B:ClassCastException C:NullPointerException D:IndexOutOfBoundsException
A:try B:catch finally C:try catch finally D:try finally
A:catch子句 B:finally子句 C:throw子句 D:try子句
A:输出:必须在1-4之间! B:输出:InputMismatchException C:什么也没输出 D:编译错误
A:ArrayIndexOutOfBoundsException B:IOException C:SQLException D:ArithmeticException
A:有继承关系的异常不能在同一个try程序段内 B:子类在先,父类在后 C:先有子类,其他如何排列都无关 D:父类在先,子类在后