一,动手动脑

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. Django:邮件功能实现

    django-users2和django的邮件功能模块都有相关的实现 ----------------------------------------------------------------- ...

  2. GoJS简单示例

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. MyEclipse 8.6.1 制作绿色版

    我们先在这个目录下新建一个文件: MyEclipse 10.6.bat , 文件内容如下: start eclipse\eclipse.exe -vm jre\bin\javaw.exe 接下来只需要 ...

  4. 算法竞赛入门经典——读书笔记day1

    1-1:整数值用%d输出,实数用%f输出. 1-2:整数/整数=整数,浮点数/浮点数=浮点数. 1-3:scanf中的占位符和变量的数据类型应一一对应,且每个变量前需要加&符号. 1-4:在算 ...

  5. C++ 11 :override 关键字的使用

    override 关键字 作用:在成员函数声明或定义中, override 确保该函数为虚函数并覆写来自基类的虚函数. 位置:函数调用运算符之后,函数体或纯虚函数标识 "= 0" ...

  6. HDU1880 魔咒词典

    题目大意:对应的输入多行,每行两个字符串,两个字符串互相映射.接下来询问的时候,如果这个字符串出现过,输出其对应的字符串. 分析:二重哈希来判断字符串是否存在,输出其对应的字符串就行.二重哈希的入门题 ...

  7. P1064 朋友数

    P1064 朋友数 转跳点:

  8. PE文件结构体-IMAGE_OPTIONAL_HEADER

    typedef struct _IMAGE_OPTIONAL_HEADER { // // Standard fields. // WORD Magic; // 标志字, ROM 映像(0107h), ...

  9. 回收 PV【转】

    当 PV 不再需要时,可通过删除 PVC 回收. 当 PVC mypvc1 被删除后,我们发现 Kubernetes 启动了一个新 Pod recycler-for-mypv1,这个 Pod 的作用就 ...

  10. python 文件与文件夹相关

    1.判断文件夹是否存在,不存在则创建文件夹: if not os.path.exists(path): os.makedirs(path) 2.判断文件是否存在,存在就删除: os.path.exis ...