动手动脑5
动手动脑1:
请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识
运行结果:
未使用异常处理的运行结果结果
添加try…catch和合适的异常处理代码后之后
结论:异常发生,运行catch中的异常处理代码,最后执行finally中的内容(不管是否有异常发生,finally语句块中的语句始终保证被执行)。
技术探索:
前页PPT中所出现之奇怪现象,可以使用javap去反汇编两个示例程序的.class文件(一个是AboutException.class,另一个是ThrowDemo.class),从中你会有所发现的。
反汇编:
动手动脑2:
阅读以下代码(CatchWho.java),写出程序运行结果:
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");
}
}
}
运行结果:
写出CatchWho2.java程序运行的结果
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");
}
}
}
运行结果:
动手动脑3:
请先阅读 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");
}
}
}
运行结果:
结论:嵌套使用try…catch语句时,最内层的finally会先执行,之后由内而外的执行。
动手动脑4:
辨析:finally语句块一定会执行吗?
请通过 SystemExitAndFinally.java示例程序回答上述问题
代码:
1 public class SystemExitAndFinally
2 {
3
4
5 public static void main(String[] args)
6
7 {
8
9 try
10 {
11
12
13 System.out.println("in main");
14
15 throw new Exception("Exception is thrown in main");
16
17 //System.exit(0);
18
19
20 }
21
22 catch(Exception e)
23
24 {
25
26 System.out.println(e.getMessage());
27
28 System.exit(0);
29
30 }
31
32 finally
33
34 {
35
36 System.out.println("in finally");
37
38 }
39
40 }
41
42
43 }
运行结果:
结论:
在catch中使用System.exit(0);程序会提前结束不会执行finally语句。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App