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=100/0; //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=0;i<sub.length();i++) { if(sub.charAt(i)>=57||sub.charAt(i)<=48)try {throw new Exception(sub);} catch (Exception e) { System.out.println("输入错误,请输入学生成绩:"); sub=in.next(); } } if(Integer.parseInt(sub)>=90&&Integer.parseInt(sub)<=100) {System.out.println("该学生成绩为:"+sub+"\n优秀呢!");} if(Integer.parseInt(sub)>=80&&Integer.parseInt(sub)<90) {System.out.println("该学生成绩为:"+sub+"\n良等!");} if(Integer.parseInt(sub)>=70&&Integer.parseInt(sub)<80) {System.out.println("该学生成绩为:"+sub+"\n中等!");} if(Integer.parseInt(sub)>=60&&Integer.parseInt(sub)<70) {System.out.println("该学生成绩为:"+sub+"\n及格喽!");} if(Integer.parseInt(sub)>=0&&Integer.parseInt(sub)<60) {System.out.println("该学生成绩为:"+sub+"\n你不及格哎!");} } }
---恢复内容结束---
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?