千锋学习的第26天:异常
一、 填空
java中所有的错误的继承自(thorwable)类;在该类中的子类中,(Error)类表示严重的底层错误,对于这类错误一般的处理方式是(不可处理);(Exception)类表示例外,异常。
二、 查询API,填空
1 异常类java.rmi.AlreadyBoundExcepion,从分类上说,该类属于(检查异常)(已检查|运行时)异常,从处理方式上来说,对这种异常(必须)处理
2 异常类java.util.regex.PatternSyntaxException,从分类上说,该类属于(运行异常)(已检查|运行时)异常,从处理方式上说,对这种异常(可处理可不处理。)处理
三、(异常的产生)把下面的代码补充完整
public class TestThrow{
public static void main(String args[]){
thorwException(10);
}
public static void throwException(int n){
if(n==0){
//抛出一个NullpointerException
throw new NullPointerException();
} else{
//抛出一个ClassCastException
//并设定详细信息为“类型转换错误”
ClassCastException cc=new ClassCastException();
System.out.println(cc.getMessage());
}
}
}
四、(try-catch-finally)有如下代码
import java.io.;
import java.sql.
class TestException{
public static void main(String args[]){
System.out.pringln(“main 1”);
int n;
//读入n
ma(n);
System.out.println(“main2”)
}
public static void ma(int n){
try{
System.out.println(“ma1”);
mb(n);
System.out.pringln(“ma2”);
}catch(EOFException e){
System.out.println(“Catch EOFException”);
}catch(IOException e){
System.out.pringln(“Catch IOException”);
}catch(SQLException e){
System.out.println(“CatchSQlExecption”);
}catch(Exception e){
System.out.println(“CatchException”);
}finally{
System.out.println(“in Finally”);
}
}
public static void mb(int n)throw Exception{
System.out.println(“mb1”);
if(n1)throw new EOFException();
if(n2)throw new FileNotFoundException();
if(n3)throw new SQLException();
if(n4)throw new NullPointerException();
System.out.println(“mb2”);
}
}
问当读入n分别为1,2,3,4,5时,输出的结果分别是什么?
**n=1
main 1
ma1
mb1
Catch EOFException
in Finally
main2
n=5
main 1
ma1
mb1
mb2
ma2
in Finally
main2**
七、(try-catch)代码改错
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”); //应该把Exception写在catch写在最后面
}catch(AruthmeticException e){
System.out.println(“ArithmeticException”);
}
}
public static void m(){
throw new MyException(); //不能抛出类型MyException的异常;异常类型必须是Throwable的子类
}
}
九、(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)
A 编译不通过
B 结果为-1
C 结果为0
十一、(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(“ln Finally”);
}
}
}
在ma中,读入一个整数b,如果读入的值为10,则输出:(“ma2+result”)。如果读入的值为0则输出(in finally);
13(异常的捕获和抛出)有以下代码
public class TestException{
public static void main(String args[]){
try{
System.out.println(“main1”);
ma();
System.out.println(“main2”);
}catch(Exception e){
System.out.println(“in Catch”);
}
}
public static void ma(){
System.out.println(“ma1”);
throw new NullPointerException();
System.out.println(“ma2”);
}
}
选择正确答案**(A)**
A编译出错
B 编译正常,输出main1 ma1 incatch
C 编译正常,运行报错
十四(异常的捕捉和抛出)有如下代码
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/处可以通过编译?(AB)
A catch(NullPointerException npe){}
B catch(IOException ioe){}
C catch(SQLException){}