锋逆战班学习第二十六天 异常练习(一)

千锋逆战班学习第26天
努力或许没有收获,但不努力一定没收获,加油。
今天我学了Java课程的集合。
中国加油!!!武汉加油!!!千锋加油!!!我自己加油!!!

1Java 中所有的错误都继承自______类;在该类的子类中, 类表示严重的底层错误,对于这类错误一般处理的方式是____;_______类表示例外、异常。

1. Throwable
2. Error
3. 不应该试图捕获它
4. Exception

2

异常类 java.rmi.AlreadyBoundException,从分类上说,该类属于__________
(已检查| 未检查)异常,从处理方式上说,对这种异常___________________;
异常类 java.util.regex.PatternSyntaxException,从分类上说,该类属于_______(已
检查|未检查)异常,从处理方式上说,对这种异常__________________
1. 已检查
2. 必须处理
3. 未检查
4. 可处理可不处理

3(异常的产生)把下面代码补充完整

public class Test3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		throwException(10);
	}
	public static void throwException(int n) {
		if(n==0) {
		//抛出一个 NullPointerException
			throw new NullPointerException();
		}else {
			//抛出一个 ClassCastException 
			//并设定详细信息为“类型转换出错”
			throw new ClassCastException("类型转换错误");
		}
	}
}

4 (try-catch-finally)有如下代码:

public class Test4 {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		System.out.println("main 1");
		int n;
		n=sc.nextInt();
		ma(n);
		System.out.println("main 2");
	}
	public static void ma(int n) {
		try {
			System.out.println("ma1");
			mb(n);
			System.out.println("ma2");
		}catch(EOFException e){
			System.out.println("Catch EOFException");
		}catch(IOException e) {
			System.out.println("Catch IOException");
		}catch(SQLException e) {
			System.out.println("Catch SQLException");
		}catch(Exception e) {
			System.out.println("Catch Exception");
		}finally {
			System.out.println("IN finaly");
		}
	}
	public static void mb(int n) throws Exception{
		System.out.println("mb1"); 
		if (n == 1) throw new EOFException(); 
		if (n == 2) throw new FileNotFoundException(); 
		if (n == 3) throw new SQLException(); 
		if (n == 4) throw new NullPointerException(); 
		System.out.println("mb2");
	}
}

问:当读入的 n 分别为 1,2,3,4,5 时,输出的结果分别是什么?
1:
main 1
ma1
mb1
Catch EOFException
In finally
main2
 
2:
main 1
ma1
mb1
Catch IOException
In finally
main2
 
3:
main 1
ma1
mb1
Catch SQLException
In finally
main2
 
4:
main 1
ma1
mb1
Catch Exception
In finally
main2
 
5:
main 1
ma1
mb1
mb2
ma2
In finally
main2

7代码改错

class MyException{} 
class TestException{ 
public static void main(String args[]){ 
ma(); 
} 
public static int ma(){ 
try{ 
m(); 
return 100; 
}catch(Exception e){ 
System.out.println(“Exception”); 
} 
catch(ArithmeticException e){ 
System.out.println(“ArithmeticException”); 
} 
} 
public static void m(){ 
throw new MyException(); 
} 
}
class MyException extends Exception{
	 public MyException(){
		  super();
		 }
		 public MyException(String msg){
		  super(msg);
		 }
}
public class Test7 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ma();
	}
	public static int ma() {
		try {
			m();
			return 100;
		}catch(ArithmeticException e) {
			System.out.println("ArithmeticException");
		}catch(Exception e) {
			System.out.println("Exception");
		}
	}
	public static void m() throws MyException {
		throw new MyException();
	}

}

9 (try-catch,局部变量)有如下代码

public class TestTryCatch{ 
public static void main(String args[]){ 
System.out.println( ma() ); 
} 
public static int ma(){ 
int n; 
try{ 
n = 10/0; 
}catch(Exception e){ 
} 
return n; 
} 
} 
选择正确答案:
A. 编译不通过
B. 编译通过,输出-1 
C. 编译通过,输出 0 

A:try执行不到的时候,n将未初始化,编译不通过

10 (try-catch-finally)有如下代码


public class TestFinally{ 
public static void main(String args[]){ 
System.out.println ( ma() ); 
} 
public static int ma(){ 
int b; 
//读入 b 
try{ 
int n = 100; 
return n/b; 
}catch(Exception e){ 
return 10; 
}finally{ 
return 100; 
} 
} 
} 
在 ma 中,当读入的 b 为 100 时,输出结果为____,当读入的 b 为 0 时,输出
结果为_____。
1.100
2.100

11(try-finally)写出下面代码运行的结果

public class TestTryFinally{ 
public static void main(String args[]){ 
try{ 
ma(); 
}catch(Exception ex1){ 
} 
} 
public static void ma() throws Exception { 
int n = 10; 
int b; 
//读入一个整数 b 
try{ 
System.out.println(“ma1”); 
int result = n / b; 
System.out.println(“ma2 ” + result); 
}finally{ 
System.out.println(“In Finally”); 
} 
} 
} 
在 ma 中,读入整数 b,如果读入的值为 10,则输出:
如果读入的值为 0,则输出:
10
md1
ma21
In Fianlly
0
md1
In Fianlly

13(异常的捕获和抛出)有以下代码:

public class Test13 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			System.out.println("main1");
			md();
			System.out.println("main2");
		}catch(Exception e) {
			System.out.println("In Catch");
		}
	}
	public static void md() {
		System.out.println("md1");
		throw new NullPointerException();
		System.out.println("md2");
	}

}
选择正确答案:
A. 编译出错
B. 编译正常,输出 main1 ma1 In Catch 
C. 编译正常,运行时出错
A抛出异常的后面的语句执行不到

14(异常的捕获和抛出)有如下代码

import java.io.*; 
import java.sql.*; 
class TestException{ 
public static void main(String args[]){ 
try{ 
ma(); 
} 
/*1*/ 
catch(Exception e){} 
} 
public static void ma() throws IOException{ } 
} 
下面哪些代码放在/*1*/处可以编译通过?
A. catch(NullPointerException npe){} 
B. catch(IOException ioe){} 
C. catch(SQLException sqle){}
AB
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值