java异常练习

java异常

选择

  1. 下列代码中的异常属于(多选)
    A. 非检查型异常   B. 检查型异常  C. Error   D. Exception
	int a = 0;
	System.out.println(2/a);

A D

  1. 类及其子类所表示的异常是用户程序无法处理的
    A. NumberFormatException   B. Exception   C. Error   D. RuntimeException

C

  1. 数组下标越界,则发生异常,提示为
    A. IOException   B. ArithmeticException   C. SQLException   D. ArrayIndexOutOfBoundsException

D

  1. 运行下列代码,当输入的num值为a时,系统会输出
    A. one three end   B. two three end   C. one two three end   D. two end
public class Test {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		try {
			int num = input.nextInt();
			System.out.println("one");
		} catch(Exception e) {
			System.out.println("two");
		} finally {
			System.out.println("three");
		}
		System.out.println("end");
	}	
}

B

  1. 运行下列代码,输出结果为
    A. a = 0   B. a = 0 除数不允许为0   C. a = 1 数组越界   D. a = 0 除数不允许为0 数组越界
public class Test {
	public static void main(String[] args) {
		try {
			int a = 1-1;
			System.out.println("a=" + a);
			int b = 4/a;
			int c[] = {1};
			c[10] = 99;
		} catch(ArithmeticException e) {
			System.out.println("除数不允许为0");
		} catch(ArrayIndexOutOfBoundsException e) {
			System.out.println("数组越界");
		} 
	}	
}

B

  1. 下列关于异常的描述,错误的是(多选)
    A. printStackTrace()用来跟踪异常事件发生时执行堆栈的内容
    B. catch块中可以出现同类型异常
    C. 一个try块可以包含多个catch块
    D. 捕获到异常后将输出所有catch语句块的内容

B D

  1. 假设要输入的id值为a101,name值为Tom,程序的执行结果为
    A. id=a101 name=Tom
    B. id=a101 name=Tom 输入结束
    C.【输入数据不合规范。Try again】
    D.【输入数据不合规范。Try again】 输入结束 java.util.InputMismatchException…
public class Test {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		try {
			int id = input.nextInt();
			String name = input.next();
			System.out.println("id=" + id + "\n" + "name=" + name);
		} catch(InputMismatchException ex) {
			System.out.println("【输入数据不合规范。Try again.】");
			System.exit(1);
			ex.printStackTrace();
		} finally {
			System.out.println("输入结束");
		} 
	}	
}

C

  1. 下列代码的运行结果为
    A. 1   B. 10   C. 20   D. 30
public class Test {
	public static int test(int b) {
		try {
			b += 10;
			
		} catch(Exception e) {
			return 1;
		} finally {
			b += 10;
			return b;
		}
	}
	public static void main(String[] args) {
		int num = 10;
		System.out.println(test(num));
	}	
	//test()方法
}

D

  1. 在下列代码划线处不可以填入选项中的哪一个异常类型
    A. Throwable   B. Exception   C. InputMismatchException   D. ArithmeticException
	//test()方法
	public static int test(int a, int b) throws ____{
		if(b == 0) {
			throw new ArithmeticException("算数异常");
		} else {
			return (a/b);
		}
	}

C

  1. 假设有自定义异常类MyException,那么抛出该异常的语句正确的是
    A. throw new Exception()
    B. throw new MyException()
    C. throw MyException
    D. throws Exception

B

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值