JAVA异常课后作业_java异常处理课后作业

一、运行AboutException.java,解释为什么会出现这样的结果

import javax.swing.*;

class AboutException {

public static void main(String[] a)

{

int i=1, j=0, k;

k=i/j;

try

{

k = i/j; //Causes division-by-zero exception

throw new Exception("Hello.Exception!");

}

catch ( ArithmeticException e)

{

System.out.println("被0除. "+ e.getMessage());

}

catch (Exception e)

{

if (e instanceof ArithmeticException)

System.out.println("被0除");

else

{

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

}

}

finally

{

JOptionPane.showConfirmDialog(null,"OK "+k);

}

}

}

(1)运行结果为

446b12afbd41e5321c93ad729f447ae2.png

(2)分析

在java中Throwable类有两个直接子类:Exception:出现的问题是可以被捕获的;Error:系统错误,通常由JVM处理。可捕获的异常又可以分为两类:(1)Check异常:直接派生自Exception的异常类,必须被捕获或再次声明抛出(2)Runtime异常:派生自RuntimeException的异常类。而AboutException.java中出现的错误就是Runtime出现的异常,

二、解释奇怪的现象

(1) int i=1, j=0, k;

k=i/j;

(2) double d1=100,d2=0,result;

result=d1/d2;

System.out.println("浮点数除以零:" + data);

(1)的代码运行时会引发异常,而(2)的代码不会引发异常!为什么?

·原因分析

因为对于(1)而言,java生成的是idiv字节码指令,而(2)java生成的是ddiv字节码指令,JVM在具体实现这两个指令是,采用了不同的处理策略,导致两段代码运行时得到不同的结果

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

public class CatchWho {

public static void main(String[] args) {

try {

try {

throw new ArrayIndexOutOfBoundsException();

}

catch(ArrayIndexOutOfBoundsException e) {

System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");

}

throw new ArithmeticException();

}

catch(ArithmeticException e) {

System.out.println("发生ArithmeticException");

}

catch(ArrayIndexOutOfBoundsException e) {

System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");

}

}

}

结果为:

e1cdc2d3a8d2cde21f42d29aff42801e.png

public class CatchWho2 {

public static void main(String[] args) {

try {

try {

throw new ArrayIndexOutOfBoundsException();

}

catch(ArithmeticException e) {

System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");

}

throw new ArithmeticException();

}

catch(ArithmeticException e) {

System.out.println("发生ArithmeticException");

}

catch(ArrayIndexOutOfBoundsException e) {

System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");

}

}

}

结果为:

2c2bfaee19d6a5054aef2b6ee01d9cdb.png

总结:

throw一个异常后,必须catch成功,才能throw下一个异常,系统不能累积处理异常!

四、多个嵌套,请先阅读 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");

}

}

}

结果为:

662e4e9ec77de1e8ce0ff70051ad6796.png

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

五、finally语句块一定会执行吗?

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");

}

}

}

637ce408018fa98cf9b70e0fa9f2e8ce.png

结果为:

不一定,因为当代码 System.exit(0);意思是,终止JAVA虚拟机,导致不能执行finally的内容!

六、课后作业

import java.util.*;

public class score {

public static void main(String[] args) {

Scanner str=new Scanner(System.in);

String score;

System.out.println("请输入成绩:");

while(true)

{

try

{

score=str.next();

if(score.matches("\\D*"))

throw new InputException();

else

{

int proo=Integer.parseInt(score);

System.out.println("输入正确!");

judge(proo);

break;

}

}

catch(InputException e)

{

System.out.println("请输入数字");

}

}

str.close();

}

public static void judge(int pro)

{

if(pro>=0&&pro<60)

System.out.println("成绩不合格!");

else if(pro<70)

System.out.println("成绩合格!");

else if(pro<80)

System.out.println("成绩中!");

else if(pro<90)

System.out.println("成绩良!");

else if(pro<=100)

System.out.println("成绩优!");

else

System.out.println("输入成绩只能在1~·100之间");

}

}

class InputException extends Exception{}

运行结果

75f8488e0920417f2ce9413cb952f94c.png

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值