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

一、动手动脑:多层的异常捕获-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. 2050编程赛 冰水挑战 HDU 6495

    题目地址:https://vjudge.net/problem/HDU-6495 思路:我们需要维护的是挑战了n个之后剩余体力值,剩余体力值越大越好dp[N(i)][N(j)],第一个维度,记录当前是 ...

  2. 接口文档注解:@ApiOperation

    @ApiOperation不是spring自带的注解是swagger里的 com.wordnik.swagger.annotations.ApiOperation; @ApiOperation和@Ap ...

  3. 与某公司CTO的一次闲聊

    这是一次与某公司CTO的交流沟通,收获不少,记录下分享给大家,其中个别词句有自己增改成分. 既然是领导,就要学会画饼,画图的都是底下干活的. 管理好别人的预期,并能兑现承诺,不能只靠画大饼忽悠.针对某 ...

  4. Hash的应用2

    代码: #include <stdio.h> #define OFFSET 500000//偏移量 ];//记录每个数是否出现,出现为1,不出现为0 int main(){ int n,m ...

  5. MyBatis select标签的用法

    From<MyBatis从入门到精通> 第一步,在接口中添加方法: public interface UserMapper { SysUser selectById(Long id); } ...

  6. Java编程思想:序列化基础部分

    import java.io.*; import java.util.Date; import java.util.Random; public class Test { public static ...

  7. ElasticSearch7.2安装

    1.环境 Java -version:java11 centos: 7.2 elasticsearch: 7.2 2.获取压缩包 wget https://artifacts.elastic.co/d ...

  8. 微信小程序 textarea 层级过高的解决方式

    建立一个新的textarea 组件代替原生textarea ,废话不多说,上代码 <template> <view class="ui-textarea"> ...

  9. Golang 高效实践之defer、panic、recover实践

    前言 我们知道Golang处理异常是用error返回的方式,然后调用方根据error的值走不同的处理逻辑.但是,如果程序触发其他的严重异常,比如说数组越界,程序就要直接崩溃.Golang有没有一种异常 ...

  10. Minikube安装成功Kubernetes,一次过!

    介绍 Minikube 是 K8S 官方为了开发者能在个人电脑上运行 K8S 而提供的一套工具.实现上是通过 Go 语言编写,通过调用虚拟化管理程序,创建出一个运行在虚拟机内的单节点集群. 注:从这里 ...