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

一、动手动脑:多层的异常捕获-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. 图片加载时间缓慢问题API

    一.背景       最近段时间,开发写值工具项目中,出现图片加载问题API,响应时间缓慢:为了优化图片加载问题,我进行图片压缩方法,然后API的图片加载还是慢,最终在自己无意中乱写找到了根本的原因. ...

  2. client-go中的golang技巧

    client-go中有很多比较有意思的实现,如定时器,同步机制等,可以作为移植使用.下面就遇到的一些技术讲解,首先看第一个: sets.String(k8s.io/apimachinery/pkg/u ...

  3. Java并发框架:Executor

    介绍 随着当今处理器中可用的核心数量的增加, 随着对实现更高吞吐量的需求的不断增长,多线程 API 变得非常流行. Java 提供了自己的多线程框架,称为 Executor 框架. 1. Execut ...

  4. 使用SpringSecurity搭建授权认证服务(1) -- 基本demo认证原理

    使用SpringSecurity搭建授权认证服务(1) -- 基本demo 登录认证是做后台开发的最基本的能力,初学就知道一个interceptor或者filter拦截所有请求,然后判断参数是否合理, ...

  5. js生成动态树状结构及排序

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. FluentValidation:一个非常受欢迎的,用于构建强类型验证规则的.NET 库

    1. FluentValidation:一个非常受欢迎的,用于构建强类型验证规则的.NET 库 请求参数实体定义: FluentValidation 验证类定义: 过滤器:ActionFilter中O ...

  7. 【搜索引擎】 PostgreSQL 10 实时全文检索和分词、相似搜索、模糊匹配实现类似Google搜索自动提示

    需求分析 要通过PostgreSQL实现类似Google搜索自动提示的功能,例如要实现一个查询海量数据中的商品名字,每次输入就提示用户各种相关搜索选项,例如淘宝.京东等电商查询 思路 这个功能可以用 ...

  8. 【POJ - 2718】Smallest Difference(搜索 )

    -->Smallest Difference 直接写中文了 Descriptions: 给定若干位十进制数,你可以通过选择一个非空子集并以某种顺序构建一个数.剩余元素可以用相同规则构建第二个数. ...

  9. py+selenium+IE10【IE已停止工作】【已解决】

    问题:跑自动化时,到某个用例IE就崩,提示已停止工作.   手工跑的时候,IE挂,提示“Internet Explorer 已经为了帮助保护您的计算机而关闭此网页”. 且每次都在需要调用flash插件 ...

  10. Jenkins+Sonar搭建持续集成和代码质量检查环境

    Jenkins+Sonar搭建 一.相关环境及下载地址 系统:Ubuntu JDK:1.8 MySQL:5.7 软件包: jenkins_2.121.3_all.deb sonarqube-7.3.z ...