---恢复内容开始---

一、动手动脑:多层的异常捕获-1

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

ArrayIndexOutOfBoundsException/内层try-catch

发生ArithmeticException

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

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

ArrayIndexOutOfBoundsException/外层try-catch

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

二、当有多个嵌套的try…catch…finally时,要特别注意finally的执行时机。

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=/; //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");
}
}
}

特别注意:

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

三、辨析: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");
}
}
}

总结:无论catch()语句有没有运行finally()语句都会执行,但如果层序的前面有Syatem.exit(1);finally()则不能执行。

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

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

1、源代码

import java.util.Scanner;
public class Grade
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("请输入学生成绩:");
String sub=in.next();
for(int i=;i<sub.length();i++)
{
if(sub.charAt(i)>=||sub.charAt(i)<=)try {throw new Exception(sub);}
catch (Exception e) {
System.out.println("输入错误,请输入学生成绩:");
sub=in.next();
}
}
if(Integer.parseInt(sub)>=&&Integer.parseInt(sub)<=)
{System.out.println("该学生成绩为:"+sub+"\n优秀呢!");}
if(Integer.parseInt(sub)>=&&Integer.parseInt(sub)<)
{System.out.println("该学生成绩为:"+sub+"\n良等!");}
if(Integer.parseInt(sub)>=&&Integer.parseInt(sub)<)
{System.out.println("该学生成绩为:"+sub+"\n中等!");}
if(Integer.parseInt(sub)>=&&Integer.parseInt(sub)<)
{System.out.println("该学生成绩为:"+sub+"\n及格喽!");}
if(Integer.parseInt(sub)>=&&Integer.parseInt(sub)<)
{System.out.println("该学生成绩为:"+sub+"\n你不及格哎!");}
}
}

---恢复内容结束---

java 动手动脑7的更多相关文章

  1. Java动手动脑——多态和继承

    Java动手动脑——继承和多态 实验一 预估输出答案:100  200  201  202 输出结果:100  200  201  202 输出答案分析:100 创建parent类的对象,调用对象的方 ...

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

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

  3. java动手动脑和动手实验

    动手动脑一: EnumTest.java: 程序代码: public class EnumTest { public static void main(String[] args) { Size s= ...

  4. java动手动脑和课后实验型问题第四讲

    1.完全"手写代码实现"随机数生成 动手动脑: 编写一个方法,使用以上算法生成指定数目(比如1000个)的随机整数. Modulus=231-1=int.MaxValue Mult ...

  5. Java动手动脑第四讲课堂作业

    动手动脑1 完全“手写代码实现”随机数生成 纯随机数发生器

  6. JAVA动手动脑多态

    动手实验一:下列语句哪一个将引起编译错误?为什么?哪一个会引起运行时错误?为什么? m=d; d=m; d=(Dog)m; d=c; c=(Cat)m; 先进行自我判断,得出结论后,运行TestCas ...

  7. JAVA动手动脑异常处理

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

  8. JAVA动手动脑

    1.运行 TestInherits.java 示例,观察输出,注意总结父类与子类之间构造方法的调用关系修改Parent构造方法的代码,显式调用GrandParent的另一个构造函数,注意这句调用代码是 ...

  9. java动手动脑和课后实验型问题

    1.以下代码的输出结果是什么?为什么会有这个结果? int[] a = { 5, 7, 20 }; System.out.println("a数组中的元素:"); // 循环输出a ...

随机推荐

  1. Xshell连接WSL

    Xshell连接WSL Windows的Windows Subsystem for Linux很好用, 可以直接使用Linux的CLI模式, 对于开发来说很友好. 安装 Windows 10系统上, ...

  2. 配置Python虚拟环境

    最小化安装的centos7中并没有安装python3 1.安装python3 1)下载安装包: wget https://www.python.org/ftp/python/3.6.2/Python- ...

  3. redis RDB 和AOF

    参考文献 Redis源码学习-AOF数据持久化原理分析(0) Redis源码学习-AOF数据持久化原理分析(1) Redis · 特性分析 · AOF Rewrite 分析 深入剖析 redis AO ...

  4. django基础知识之中间件:

    中间件 是一个轻量级.底层的插件系统,可以介入Django的请求和响应处理过程,修改Django的输入或输出 激活:添加到Django配置文件中的MIDDLEWARE_CLASSES元组中 每个中间件 ...

  5. 从URL获取图片并保存到本地

    /// <summary> /// HttpWebRequest Property /// </summary> /// <param name="fileNa ...

  6. goroutine上下文切换机制

    goroutine是go语言的协程,go语言在语言和编译器层面提供对协程的支持.goroutine跟线程一个很大区别就是线程是操作系统的对象,而goroutine是应用层实现的线程.goroutine ...

  7. 基于SpringBoot-Dubbo的微服务快速开发框架

    简介: 基于Dubbo的分布式/微服务基础框架,为前端提供脚手架开发服务,结合前一篇--Web AP快速开发基础框架,可快速上手基于Dubbo的分布式服务开发,项目代码: https://github ...

  8. 七牛云图床和Markdown使用

    七牛云图床和Markdown使用 1.图床是什么? 图床一般是指储存图片的服务器,有国内和国外之分.国外的图床由于有空间距离等因素决定访问速度很慢影响图片显示速度.国内也分为单线空间.多线空间和cdn ...

  9. GitHub使用整理——关于上传Keil工程一些注意的点

    git上传警告warning: LF will be replaced by CRLF 在上传keil工程时,会遇到warning: LF will be replaced by CRLF警告: wa ...

  10. CF1194D 1-2-K Game (博弈论)

    CF1194D 1-2-K Game 一道简单的博弈论题 首先让我们考虑没有k的情况: 1. (n mod 3 =0) 因为n可以被分解成若干个3相加 而每个3可以被分解为1+2或2+1 所以无论A出 ...