博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
动手动脑(异常处理)
阅读量:6922 次
发布时间:2019-06-27

本文共 4892 字,大约阅读时间需要 16 分钟。

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

源代码

import javax.swing.*;public 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");     }          }}

实验结果截图

2.请尝试解释以下奇怪现象

jvm在处理浮点数时,生成的是ddiv字节码指令,i/0,0转化为浮点数0.0,而0.0是double类型的,并不精确,所以不会抛出异常。

jvm在处理整数时,生成的是idiv字节码指令,整数除0就是除0,会抛出异常。

3.阅读以下代码(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");         }     } }

结果截图

4.写出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");         }     } }

结果截图

try catch这一模式,是有顺序依据的,当执行try语句是,紧接着的就是所对应的catch来执行,然后接着catch继续执行下去。

5.请先阅读 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是无论是否出现异常都会执行的,在第三个try中出现异常,紧跟着的catch已经接收到,但此并不算第二个try出现异常,因为异常已经解决,那么之后就不会显示第二个和第一个catch的内容了。

6.辨析: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");                }        }}

结果截图:

JVM是java虚拟机,finally是由JVM保证执行,而System.exit(0)是正常退出程序,结束JVM的运行,那么最后finally就不再执行。

    finally语句不被执行的唯一情况是先执行了用于终止程序的System.exit()方法。

转载于:https://www.cnblogs.com/liujinxin123/p/9938671.html

你可能感兴趣的文章
陈松松:视频营销效果不好,从这四个角度找原因
查看>>
Python代码调试技巧
查看>>
Nginx优化指南
查看>>
elasticsearch集群搭建手册
查看>>
如何正确认识XMind软件格式
查看>>
OSChina 周一乱弹 ——有2个小混蛋大晚上……
查看>>
JNI/NDK开发指南(二)——JVM查找java native方法的规则
查看>>
Installing / Updating Python on OS X
查看>>
Android解析XML文件
查看>>
Spring AOP前置通知和后置通知
查看>>
Windows 之间用rsync同步数据(cwRsyncServer配置)
查看>>
android gps定位
查看>>
JFinal AOP学习笔记
查看>>
idea 查看Java类字节码 自定义工具的使用
查看>>
“效果系列一”:jQuery 手风琴效果
查看>>
java相对路径获取(读取)文件
查看>>
VS上写helloworld
查看>>
反射机制——获取Class对象的三种方式
查看>>
react-native 初始化指定版本
查看>>
怎样看服务器负载状况好坏
查看>>