首页 技术 正文
技术 2022年11月9日
0 收藏 893 点赞 3,268 浏览 3143 个字

—恢复内容开始—

一、动手动脑:多层的异常捕获-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");
}
}
}

java  动手动脑7

动手动脑:多层的异常捕获-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");
}
}
}

java  动手动脑7

二、当有多个嵌套的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");
}
}
}

java  动手动脑7

特别注意:

当有多层嵌套的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");
}
}
}

java  动手动脑7

总结:无论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

—恢复内容结束—

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,294