一,动手动脑

1,请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识。

import javax.swing.*;

class AboutException {
public static void main(String[] a)
{
int i=1, j=0, k;
//k=i/j;
try
{
k = i/j; // Causes division-by-zero exception
throw new Exception("Hello.Exception!");
} catch ( ArithmeticException e)
{
System.out.println("除0. "+ e.getMessage());
} catch (Exception e)
{
if (e instanceof ArithmeticException)
System.out.println("除0");
else
{
System.out.println(e.getMessage()); }
} finally
{
JOptionPane.showConfirmDialog(null,"OK");
} }
}

运行测试

总结:

1,把可能会发生错误的代码放进try语句块中。

2,当程序检测到出现了一个错误时会抛出一个异常对象。异常处理代码会捕获并处理这个错误。 catch语句块中的代码用于处理错误。

3,当异常发生时,程序控制流程由try语句块跳转到catch语句块。

4,不管是否有异常发生,finally语句块中的语句始终保证被执行。

5,如果没有提供合适的异常处理代码,JVM将会结束掉整个应用程序。

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");
}
}
}

请先阅读 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语句块一定会执行吗?请通过 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"); } } }

多层异常捕获总结:

1,当有多个嵌套的try…catch…finally时,try是从最外层向最里层依次执行,finally是从最里层向最外层依次执行(类似于栈:try=入栈,finally=出栈)。

2,当有多层嵌套的finally时,异常在不同的层次抛出,在不同的位置抛出,可能会导致不同的finally语句块执行顺序。

3,try-catch-finally相互嵌套时,先处理最内层的try-catch-finally。当try抛出了与catch匹配的异常,则代码到相应的catch()中执行。如果catch也出现了异常,程序会检测finally中是否有异常,若有,则覆盖。如果只有try-finally,那么先执行finally,如果finally没有异常,则返回处理try中的异常,如果finally有异常,则覆盖try中的异常

4,finally语句块不一定会执行(如:System.exit(0);强行在finally之前终止程序运行)。

java04异常处理课堂总结的更多相关文章

  1. 课堂动手动脑验证以及自定义异常类实现对异常处理——java异常类

    异常(exception):发生在程序执行期间,表明出现了一个非法运行的情况.许多JDK中的方法在检测到非法情况时,都会抛出一个异常对象.例如:数组越界和被0除. 代码验证: package test ...

  2. Java中的异常处理try catch(第八周课堂示例总结)

    异常处理 使用Java异常处理机制: 把可能会发生错误的代码放进try语句块中. 当程序检测到出现了一个错误时会抛出一个异常对象. 异常处理代码会捕获并处理这个错误. catch语句块中的代码用于处理 ...

  3. java-04类和对象课堂练习

    1.请运行并输入以下代码,得到什么结果 public class Test { public static void main(String[] args){ Foo obj1=new Foo(); ...

  4. 小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_17、SpringBootTest单元测试实战

    笔记 1.@SpringBootTest单元测试实战     简介:讲解SpringBoot的单元测试         1.引入相关依赖              <!--springboot程 ...

  5. java课堂作业--异常处理

    一. 运行结果: 二. 结果: ArrayIndexOutOfBoundsException/内层try-catch 发生ArithmeticException 三. 结果: ArrayIndexOu ...

  6. 小课堂Week10 例外处理设计的逆袭Part3

    小课堂Week10 例外处理设计的逆袭Part3 今天是<例外处理设计的逆袭>这本书阅读的第三天,也是最后一天,我们会主要通过实例,对Part2中提出的例外处理等级进行解读. Level1 ...

  7. 小课堂Week9 例外处理设计的逆袭Part2

    小课堂Week9 例外处理设计的逆袭Part2 今天继续阅读<例外处理设计的逆袭>这本书,我们先看两个案例: 案例1 问:如果要设计一个依据学号到数据库中查询学生资料的函数,当找不到符合条 ...

  8. Spark小课堂Week4 从控制台看Spark逻辑结构

    Spark小课堂Week4 从控制台看Spark逻辑结构 层级关系: 从监控控制台,我们可以看到如下关系: 一个 Job 包含 n Stage 一个 Stage 包含 n Task Job0解决什么问 ...

  9. 异常处理第一讲(SEH),筛选器异常,以及__asm的扩展,寄存器注入简介

    异常处理第一讲(SSH),筛选器异常,以及__asm的扩展 博客园IBinary原创  博客连接:http://www.cnblogs.com/iBinary/ 转载请注明出处,谢谢 一丶__Asm的 ...

随机推荐

  1. CNN反向传播算法公式

    网络结构(6c-2s-12c-2s): 初始化: \begin{align}\notag W \sim U(- \frac{\sqrt{6}}{\sqrt{n_j+n_{j+1}}} , \frac{ ...

  2. d3基本图形

                                             柱状图                                   散点图.气泡图               ...

  3. 「NOIP2017」列队

    传送门 Luogu 解题思路 一眼平衡树,应该没问题吧? 但我们一定要反应过来,单点的维护是非常之困难的,因为这是一个网格图而不仅仅是一条序列. 我们要考虑把修改操作全都放在序列上进行. 其实题面里是 ...

  4. 「CF741D」Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths

    传送门 Luogu 解题思路 考虑把22个字符状压下来,易知合法情况就是状态中之多有一个1,这个可以暴力一点判断23次. 然后后就是 dsu on the tree 了. 细节注意事项 咕咕咕 参考代 ...

  5. dotnet-千星项目OpenAuthNet基于NetCore21的快速开发框架

    下载

  6. 吴裕雄--天生自然JAVAIO操作学习笔记:IO操作实例、Scanner、数据操作流与合并流

    import java.io.* ; public class ExecDemo01{ public static void main(String args[]) throws Exception{ ...

  7. 题解 loj2065 「SDOI2016」模式字符串

    点分治. 考虑经过当前分治中心\(u\)的点对数量. 这种数点对数的问题,有一个套路.我们可以依次考虑\(u\)的每个儿子,看用当前的儿子,能和之前已经考虑过的所有儿子,组成多少点对.这样所有合法的点 ...

  8. NO23 Linux正则表达式结合三剑客企业级实践--取IP

    企业实践: 一.取IP的方法(用三剑客): grep: awk: sed:虽有三种,但是思路是一样的,用到正则有些表达细节不一样而已. 分析: sed***: 课堂试题: |sed -nr 's#^. ...

  9. JAVA开源爬虫列表及简介

    本文列举了一些较为常用的JAVA开源爬虫框架: 1.Apache Nutch 官方网站:http://nutch.apache.org/ 是否支持分布式:是 可扩展性:中.Apache Nutch并不 ...

  10. swoole之创建子进程

    一.代码 <?php /** * 进程就是正在运行的程序的一个实例 * 比如,在某个终端中执行一个PHP脚本,可以认为就是开启了一个进程,会有对应的进程id(pid) * * swoole进程与 ...