• java中的异常处理机制


    java异常处理机制

    1)在java语言中,通常将可能出现异常的语句放入try{}语句中,将出现错误后需要执行的语句放入到catch{}语句中,将无论是否发生异常都要执行的语句放在finally{}语句中。

    2)当程序执行出现异常的时候,系统会抛出一个异常,然后由try{}语句中中出现异常的地方转到catch{}语句中。不过不管有没有异常产生,finally{}中的语句都将执行。

    3)如果系统出现系统错误或者运行Runtime异常,jvm会结束程序运行,不一定会执行finally{}中的语句。

    4)如果try{}中产生的异常在catch中没有处理,系统将停止程序,也不会执行finally中的语句。

     

     

    多层异常处理

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

            }

        }

    }

           程序运行结果:

    ArrayIndexOutOfBoundsException/内层try-catch

    发生ArithmeticException

          

       分析:在本程序中,一个外部try{}catch{}语句中嵌套了一个try{}catch{}语句。内部的try{}语句中抛出一个ArrayIndexOutOfBoundsException,然后内部的catch{}语句捕捉到该异常并进行处理。然后外部try{}语句中抛出一个ArithmeticException异常,外部catch{}捕获异常进行处理。在程序中可以看到,外部共有两个catch{}语句。一个用来捕获ArrayIndexOutOfBoundsException异常,另一个用来捕获ArithmeticException,可是最后结果却是外部catch并没有捕获ArrayIndexOutOfBoundsException,只捕获了ArithmeticException异常,这是因为在内部的catch语句中已经将ArrayIndexOutBoundsException处理了。再来看以下代码:

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

            }

        }

    }

     

    运行结果

    ArrayIndexOutOfBoundsException/外层try-catch

    分析:在此程序中,内部try{}抛出的异常ArrayIndexOutOfBoundsException在内部catch语句中没有得到处理,所以外部try{}语句中抛出ArithmeticException的语句并没有执行,在外部的catch语句中ArrayIndexOutOfBoundsException被捕获所以执行了第二个catch{}语句中的内容。

     

     

     

    多个try{}catch{}finally{}语句嵌套

       当有多个try{}catch{}finally{}语句嵌套使用的首,根据异常在不同层次不同位置的抛出,是会造成finally语句不同的执行结果,这里的情况比较多,较为繁琐不做具体分析,但是总体原则是:一个异常抛出的语句到捕获这个异常的catch语句之间的语句,不论是try,catch,finally语句都不会执行。

     

     

     

     

    关于finally语句是否一定会执行

    请看以下代码:

    public class SystemExitAndFinally {

           public static void main(String[] args)

        {       

                  try{

               System.out.println("in main");

                         throw new Exception("Exception is thrown in main");

          }catch(Exception e) {

                         System.out.println(e.getMessage());

                         System.exit(0);

                  }finally{

                         System.out.println("in finally");

                  }

          }

    }

     

     

    执行结果为:

    in main

    Exception is thrown in main

    可见也不是任何情况下finally语句中的代码都会执行,此次是因为在处理异常的catch代码中执行了退出的方法。

  • 相关阅读:
    MSSQL查询表占用空间
    JS字典
    匹配是否指定主域名
    站点文件删除不了提示权限不足
    事件委托发布-订阅
    微信中打开第三方应用
    以Spring Bean配置文件为例解释 xmlns,xmlns:xsi,xsi:schemaLocation
    Hdfs的读出机制
    Hdfs的写入机制
    spring常用注解的作用
  • 原文地址:https://www.cnblogs.com/maosonglin/p/6096442.html
Copyright © 2020-2023  润新知