• Java中常用的异常处理情况课件动手动脑


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

    }
    }

    运行结果如下:

    Java中把可能会发生错误的代码放进try语句块中。 当程序检测到出现了一个错误时会抛出一个异常对象。异常处理代码会捕获并处理这个错误。 catch语句块中的代码用于处理错误。 当异常发生时,程序控制流程由try语句块跳转到catch语句块。 不管是否有异常发生,finally语句块中的语句始终保证被执行。 如果没有提供合适的异常处理代码,JVM将会结束掉整个应用程序。

    Throwable类有两个直接子类: Exception:出现的问题是可以被捕获的; Error:系统错误,通常由JVM处理。 可捕获的异常又可以分为两类: (1)Check异常:直接派生自Exception的异常类,必须被捕获或再次声明抛出 (2)Runtime异常:派生自RuntimeException的异常类。使用throw语句可以随时抛出这种异常对象: throw new ArithmeticException(…);

    JDK1.4 以上提供了assert语句,允许程序在运行期间判断某个条件是否满足,不满足时,抛出AssertionError

    默认情况下,assert功能是关闭的,可以在使用java启动JVM时添加参数-ea打开它。

    2:int i=1, j=0, k; k=i/j;代码在运行时 会引发异常,而double d1=100,d2=0,result; result=d1/d2; System.out.println("浮点数除以零:" + data);不会引发异常,原因是什么?

    JVM在具体实现这两个程序的字节码指令时,采用了不同的处理策略,所以会导致两段代码运行时得到不同的结果。

    可以有多个catch语句块,每个代码块捕获一种异常。在某个try块后有两个不同的catch 块捕获两个相同类型的异常是语法错误。 使用catch语句,只能捕获Exception类及其子类的对象。因此,一个捕获Exception对象的catch语句块可以捕获所有“可捕获”的异常。 将catch(Exception e)放在别的catch块前面会使这些catch块都不执行,因此Java不会编译这个程序。

    “finally”的功用:资源泄露:当一个资源不再被某应用程序使用,但此程序并未向系统声明不再使用此资源时发生这种情况 finally语句块主要用于解决资源泄露问题,它位于catch语句块之后,JVM保证它们一定执行。 注意:finally语句块中也可能发生异常,如果这种情况发生,先前的异常被放弃。

    3:阅读以下代码(CatchWho.java),写出程序运行结果以及写出CatchWho2.java程序运行的结果。再阅读EmbedFinally.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");
    }
    }
    }

    运行结果如下:

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

    运行结果如下:

    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时,要特别注意finally的执行时机。

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

    4:finally语句块一定会执行吗?

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

    }

    }


    }

    运行结果如下:

    如果在try代码块之前就结束了temp()方法,try代码块并没有得到执行,所以finally中的代码块也不会得到相应的执行。只有在try代码块得到执行的情况下,finally代码块才会得到执行。如果当一个线程在执行 try 语句块或者 catch 语句块时被打断(interrupted)或者被终止(killed),与其相对应的 finally 语句块可能不会执行。还有更极端的情况,就是在线程运行 try 语句块或者 catch 语句块时,突然死机或者断电,finally 语句块肯定不会执行了。

  • 相关阅读:
    Maven配置阿里云镜像
    docker启动报错 error creating overlay mount
    docker 运行jenkins容器
    centos7 安装docker
    Create PropertyManager Page Example (VB.NET)
    Solidworks api帮助代码报错 “特性说明符不是一个完整的语句。请使用行继续符将该特性应用于下列语句。“的解决办法!
    Solidworks API中在一个拉伸的矩形的前后面建立两个基准面的代码示例
    用vb.net运行一个应用程序,以solidworks为例
    solidworks二次开发中直接录出来的VB代码拿到自己建的项目里出现“未设置对象变量或with块变量”解决办法!
    visual studio xamarin 离线安装文件以及 android 模拟器
  • 原文地址:https://www.cnblogs.com/fuheishi/p/9941560.html
Copyright © 2020-2023  润新知