未分类
2020-08-27 14:24:25
1822677238@qq.com
手机扫码查看
异常课后习题
1.填空
java中所有的错误都继承自Throwable类;
在该类的子类中,Error类表示严重的底层错误,对于这类错误一般处理的方法是重新书写重新编译重新运行;Exception类表示例外、异常
2.查询API,填空
– 异常类java.rmi.AlreadyBoundException,从分类上说,该类属于受查异常,从处理方式上说,对这种异常必须处理;
– 异常类java.util.regex.PatternSyntaxException,从分类上说,该类属于运行时异常,从处理方式上说,对这种异常可处理也可不处理。
3.写出结果
当读入的 n 分别为 1,2,3,4,5 时,输出的结果分别是什么?
import java.io.*;
import java.sql.SQLException;
public class demos{
public static void main(String[] args) {
System.out.println("main 1");
int n=1;
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 finally");
}
}
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:
main 1
ma1
mb1
Catch EOFException
In finally
main2
n==2:
main1
ma1
mb1
Catch Exception
In finally
main2
n==3:
main1
ma1
mb1
Catch SQLException
In finally
main2
n==4:
main1
ma1
mb1
Catch Exception
In finally
main2
n==5:
main1
ma1
mb1
mb2
ma2
In finally
main2
*/
4.try-catch,局部变量,以下代码是否通过
public class demos{
public static void main(String[] args) {
System.out.println(ma());
}
public static int ma(){
//程序不通过,因n未正确赋值
int n;
try{
n=10/0;
}catch (Exception e){}
return n;
}
}
5.try-catch-finally
public class demos{
public static void main(String[] args) {
System.out.println(ma());
/*
在 ma 中,当读入的 b 为 100 时,输出结果为多少,当读入的 b 为 0 时,输出结果为多少
*/
}
public static int ma(){
int n=0;
try{
int b=100;
return b/n;
}catch (Exception e){
return 10;
}finally {
return 100;
}
}
}
6.以下代码是否能编译通过?如果不能,应该如何修改?
import java.io.*;
class MySuper{
public void m() throws IOException{}
}
class MySub extends MySuper{
@Override
public void m() throws EOFException{}
}
class MySub2 extends MySub{
@Override
/*编译错误,该异常属于IO异常子类,不属于EOFException子类
从MySub继承不能比MySub抛出更多的异常
*/
public void m() throws FileNotFoundException{}
}
- 本页地址 https://www.9713job.com/?p=2197
- 上一篇 <<2020java教程:异常
- 下一篇 >>java教程:JVM的存储区域以及JVM栈详解(栈帧结构)



发表回复