java 动手动脑7
---恢复内容开始---
一、动手动脑:多层的异常捕获-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的更多相关文章
- Java动手动脑——多态和继承
Java动手动脑——继承和多态 实验一 预估输出答案:100 200 201 202 输出结果:100 200 201 202 输出答案分析:100 创建parent类的对象,调用对象的方 ...
- java动手动脑和课后实验型问题String类型
1.请运行以下示例代码StringPool.java,查看其输出结果.如何解释这样的输出结果?从中你能总结出什么? true true false 总结: 使用new关键字创建字符串对象时, 每次申请 ...
- java动手动脑和动手实验
动手动脑一: EnumTest.java: 程序代码: public class EnumTest { public static void main(String[] args) { Size s= ...
- java动手动脑和课后实验型问题第四讲
1.完全"手写代码实现"随机数生成 动手动脑: 编写一个方法,使用以上算法生成指定数目(比如1000个)的随机整数. Modulus=231-1=int.MaxValue Mult ...
- Java动手动脑第四讲课堂作业
动手动脑1 完全“手写代码实现”随机数生成 纯随机数发生器
- JAVA动手动脑多态
动手实验一:下列语句哪一个将引起编译错误?为什么?哪一个会引起运行时错误?为什么? m=d; d=m; d=(Dog)m; d=c; c=(Cat)m; 先进行自我判断,得出结论后,运行TestCas ...
- JAVA动手动脑异常处理
1>请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识. import javax.swing.*; class AboutEx ...
- JAVA动手动脑
1.运行 TestInherits.java 示例,观察输出,注意总结父类与子类之间构造方法的调用关系修改Parent构造方法的代码,显式调用GrandParent的另一个构造函数,注意这句调用代码是 ...
- java动手动脑和课后实验型问题
1.以下代码的输出结果是什么?为什么会有这个结果? int[] a = { 5, 7, 20 }; System.out.println("a数组中的元素:"); // 循环输出a ...
随机推荐
- vue中v-model的数据双向绑定(重要)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 用SpringBoot+MySql+JPA实现对数据库的增删改查和分页
使用SpringBoot+Mysql+JPA实现对数据库的增删改查和分页 JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述 ...
- C++学习书籍推荐《More Exceptional C++(英文)》下载
百度云及其他网盘下载地址:点我 作者简介 Herb Sutter is the author of three highly acclaimed books, Exceptional C++ Styl ...
- 前端经常碰到的小知识点-----js篇
一 js 1.可视区宽和高 ① document.documentElement.clientWidth //可视区的宽度 document.documentElement.clientHei ...
- getlasterror() 输出错误信息,
得自http://bbs.csdn.net/topics/390416234 LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE ...
- C#7.0 新增功能
连载目录 [已更新最新开发文章,点击查看详细] C# 7.0 向 C# 语言添加了许多新功能 01 out 变量 支持 out 参数的现有语法已在此版本中得到改进. 现在可以在方法调用的参数列表 ...
- C#3.0新增功能01 自动实现的属性
连载目录 [已更新最新开发文章,点击查看详细] 在 C# 3.0 及更高版本,当属性访问器中不需要任何其他逻辑时,自动实现的属性会使属性声明更加简洁. 它们还允许客户端代码创建对象. 当你声明以 ...
- 使用RabbitMQ做数据接收和处理时,自动关闭
场景:N个客户端向MQ里发送数据:服务器上有另一个控制台程序(假设叫ServerClient)来处理这里数据(往数据库保存).方向为Client * n→MQSERVER→ServerClient 问 ...
- 从0系统学Android-2.4隐式Intent
本系列文章,参考<第一行代码>,作为个人笔记 更多内容:更多精品文章分类 使用隐式 Intent 相对于显示 Intent ,隐式 Intent 比较含蓄.这种方式不明确指出我们想要启动哪 ...
- 蓝牙Beacon广播数据包格式以及解析
目录 1. 获取原始蓝牙广播包 2. 安装WireShark软件 3. 分析Beacon广播包数据 3.1 第一个数据包格式 3.2 第二个数据包格式 3.3 Android程序开发中的蓝牙广播包 4 ...