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

1)  源代码

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

}

}

}

2)  运行结果

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

1、1)源代码

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

}

}

}

1)  运行结果

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

1)源代码

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

}

}

}

2)运行结果

1)  总结

先执行try语句,有catch执行,最后执行finally语句。

4、辨析:finally语句块一定会执行吗?请通过 SystemExitAndFinally.java示例程序回答上述问题

1)源代码

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

}

}

}

2)运行结果

3)总结

Finally语句不一定执行,在catch语句中,有exit(0)语句即退出。要看具体情况,catch语句的处理。

5、编写一个程序,此程序在运行时要求用户输入一个整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。要求程序必须具备足够的健壮性,不管用户输入什么样的内容,都不会崩溃。

1)源代码

import java.io.*;

public class Grade {

public static void main(String[] args) {

// TODO Auto-generated method stub

String a = null;

int g;

BufferedReader strin =new BufferedReader(new InputStreamReader(System.in));

try

{

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

a =  strin.readLine();

}

catch(Exception e)

{

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

}

try

{

g = Integer.parseInt(a);

if(g<=100&&g>=90)

System.out.println("优秀");

else if(g>=80&&g<90)

System.out.println("良好");

else if(g>=70&&g<80)

System.out.println("中");

else if(g>=60&&g<70)

System.out.println("及格");

else if(g<60)

System.out.println("不及格");

else

System.out.println("分值错误");

}

catch(NumberFormatException e)

{

System.out.println("输入错误");

}

}

}

2)运行结果

JAVA 多态和异常处理作业——动手动脑以及课后实验性问题的更多相关文章

  1. JAVA 接口与继承作业——动手动脑以及课后实验性问题

    一.继承条件下的构造方法调用 运行 TestInherits.java 示例,观察输出,注意总结父类与子类之间构造方法的调用关系修改Parent构造方法的代码,显式调用GrandParent的另一个构 ...

  2. JAVA类与对象作业——动手动脑以及课后实验性问题

    一: 1.源代码 //MAO2015.10.6 //foo class Foo { int value; public Foo(int initValue) { value = initValue; ...

  3. JAVA 数组作业——动手动脑以及课后实验性问题

    JAVA课后作业——动手动脑 一:阅读并运行示例PassArray.java,观察并分析程序输出的结果,小结,然后与下页幻灯片所讲的内容进行对照. 1.源代码 // PassArray.java // ...

  4. JAVA语法基础作业——动手动脑以及课后实验性问题 (八)

    一.动手动脑 运行AboutException.java示例,了解Java中实现异常处理的基础知识. 1)源代码 import javax.swing.*; class AboutException ...

  5. JAVA String作业——动手动脑以及课后实验性问题

    一:如何解释这样的输出结果?从中你能总结出什么?1.源码 //mao2015.10.20 //==的两个分辨 //对原始数据而言 //对引用类型变量 public class StringPool { ...

  6. 2019-9-16 java上课知识整理总结(动手动脑,课后实验)

    java上课知识整理总结(动手动脑,课后实验) 一,课堂测试 1,题目:课堂测试:像二柱子那样,花二十分钟写一个能自动生成30道小学四则运算题目的 “软件” 要求:(1)题目避免重复: (2)可定制( ...

  7. Java(异常处理)动手动脑

    1>请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识. import javax.swing.*; class AboutEx ...

  8. java动手动脑和课后实验型问题String类型

    1.请运行以下示例代码StringPool.java,查看其输出结果.如何解释这样的输出结果?从中你能总结出什么? true true false 总结: 使用new关键字创建字符串对象时, 每次申请 ...

  9. JAVA09异常处理之动手动脑问题

    动手动脑1:为什么不管是否有异常发生,finally语句块中的语句始终保证被执行? 我们在写代码时,如果finally块中的代码过多会导致字节码条数"膨胀",因为finally中的 ...

随机推荐

  1. Cheatsheet: 2015 04.01 ~ 04.30

    Other CentOS 7.1 Released: Installation Guide with Screenshots A Git Style Guide Recommender System ...

  2. ubuntu Linux 测试PHP却提示下载文件的解决办法

    ubuntu Linux 测试PHP却提示下载文件的解决办法   一般这种情况都是在刚刚开始配置环境时出现的, 输入 sudo a2enmod php5  看提示如果出现“$ This module ...

  3. albert1017 Linux下压缩某个文件夹(文件夹打包)

    albert1017 Linux下压缩某个文件夹(文件夹打包) tar -zcvf /home/xahot.tar.gz /xahottar -zcvf 打包后生成的文件名全路径 要打包的目录例子:把 ...

  4. python tools: iPython Notebook

    Introducing IPython Notebook IPython isn't a different programming language, it's just a set of comp ...

  5. How to crack interviews ...

    Code practice: Leetcode: www.leetcode.com HackerRank: www.hackerrank.com Topcoder: https://www.topco ...

  6. python_way,day8 面向对象【多态、成员--字段 方法 属性、成员修饰符、特殊成员、异常处理、设计模式之单例模式、模块:isinstance、issubclass】

    python_way day8 一.面向对象三大特性: 多态 二.面向对象中的成员 字段.方法属性 三.成员修饰符 四.特殊成员 __init__.__doc__.__call__.__setitem ...

  7. iOS - MPMoviePlayer 视频播放

    前言 MP_EXTERN_CLASS_AVAILABLE(3_2) NS_DEPRECATED_IOS(3_2, 9_0, "Use AVPlayerViewController in AV ...

  8. Redis常用命令入门——列表类型(一级二级缓存技术)

    获取列表片段 redis > LRANGE KEY_NAME START END lrange命令比较常用,返回从start到stop的所有元素的列表,start和stop都是从0开始. (1) ...

  9. nodejs学习笔记<五>npm使用

    NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题. 以下是几种常见使用场景: 允许用户从NPM服务器下载别人编写的第三方包到本地使用. 允许用户从NPM服务器下载并 ...

  10. LCD控制器与驱动器

    这回我再讲讲从 MCU 到 LCD 之间是怎样一个控制流程,即我们的位图数据是怎样显示到 LCD 上的.前面我们了解到 LCD 显示是用动态扫描的方式来实现的,每次显示一整行,在一帧里每行一次扫描一遍 ...