异常处理动手动脑问题

时间:2022-05-28 02:55:04

 

1.      阅读以下代码(CatchWho.java),写出程序运行结果:

异常处理动手动脑问题

运行结果:

异常处理动手动脑问题

2.    写出CatchWho2.java程序运行的结果

异常处理动手动脑问题

运行结果:

异常处理动手动脑问题

3.请先阅读 EmbedFinally.java示例,再运行它,观察其输出并进行总结。

public class EmbededFinally {


public static void main(String args[]) {

int result;

try {

System.out.println(
"in Level 1");


try {

System.out.println(
"in Level 2");
// result=100/0; //Level 2

try {

System.out.println(
"in Level 3");

result
=100/0; //Level 3

}

catch (Exception e) {

System.out.println(
"Level 3:" + e.getClass().toString());

}


finally {

System.out.println(
"In Level 3 finally");

}


// result=100/0; //Level 2


}

catch (Exception e) {

System.out.println(
"Level 2:" + e.getClass().toString());

}
finally {

System.out.println(
"In Level 2 finally");

}

// result = 100 / 0; //level 1

}

catch (Exception e) {

System.out.println(
"Level 1:" + e.getClass().toString());

}

finally {

System.out.println(
"In Level 1 finally");

}

}

}

异常处理动手动脑问题

结果分析:

1)当有多层嵌套的finally时,异常在不同的层次抛出    ,在不同的位置抛出,可能会导致不同的finally语句块执行顺序。

2)try抛出一个异常之后,程序会跳出try,不再执行try后边的语句,开始对catch进行匹配,处理异常;

3)try嵌套中,抛出的异常只有被处理才可以按顺序抛出下一个异常,如果不处理,程序就终止;

4)try抛出异常之后,就跳出了try语句,内层catch无法捕获就继续向外抛,所以外层也就有异常,外层语句不执行,第二个程序 throw  new ArithmeticExcepption没有执行。

5)第三个程序,try第一层第二层没有异常不用捕获,执行完之后到第三层,除0有异常,catch捕获,执行第三层的finally然后,顺序执行第二层,第一层的finally。

4. 辨析:finally语句块一定会执行吗?

请通过 SystemExitAndFinally.java示例程序回答上述问题

public class SystemExitAndFinally {


public static void main(String[] args)
{

try{


System.out.println(
"in main");

throw new Exception("Exception is thrown in main");

//System.exit(0);


}

catch(Exception e)

{

System.out.println(e.getMessage());

System.exit(
0);

}

finally
{

System.out.println(
"in finally");

}

}
}

异常处理动手动脑问题

结论:

1)try语句嵌套从外层到内层执行,在try语句中,哪一层出错,哪一层就抛出异常,后边的try语句就不再执行,如果该层存在catch就进行相应的捕获,有该层的finally也执行,除非finally遇到不执行的情况;

2)如果该层没有catch进行捕获,就向外抛出,去找catch,如果没有catch进行捕获,就终止程序。

5. 编写一个程序,此程序在运行时要求用户输入一个  整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。

要求程序必须具备足够的健壮性,不管用户输入什      么样的内容,都不会崩溃。

源代码:

import java.util.Scanner;
//信1605-3 20163693 王晓丹
class ChulicuowuException extends Exception{
ChulicuowuException(){
System.out.println(
"所输入的成绩超出范围!");

}
}
class shuru{
private int score;
public int getScore()
{
return score;
}
public void setScore(int score) {
this.score=score;
}
public void panDuanFanWei() throws ChulicuowuException {
if(score>100||score<0) {
ChulicuowuException limiteRange
=new ChulicuowuException();
throw limiteRange;
}
}
public void shuruzhengshu() {
Scanner input
=new Scanner(System.in);
System.out.print(
"请输入一个整数:");
if(input.hasNextInt()) {
score
=input.nextInt();
}
else {
System.out.println(
"输入的不是整数");
System.exit(
0);
}

try {
panDuanFanWei();
}
catch (ChulicuowuException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}
}
}
public class TestScore {

public static void main(String[] args) throws ChulicuowuException {
// TODO Auto-generated method stub
shuru S=new shuru();
S.shuruzhengshu();
S.panDuanFanWei();
if(S.getScore()<60)
System.out.print(
"不及格");
else {
System.out.print(
"及格");
if(S.getScore()<70)
System.out.print(
"中");
else if(S.getScore()<90) {
System.out.print(
"良");
}
else if(S.getScore()<100) {
System.out.print(
"优");
}
}
}

}

异常处理动手动脑问题

异常处理动手动脑问题

异常处理动手动脑问题