zoukankan      html  css  js  c++  java
  • try catch finally的一些用法

    一:例题:

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

    其结果显示

    被0除.  / by zero

    总结:程序看下来,1/0在数学逻辑上是错误的,在计算机运行中肯定会报错,当编程过程中,有些地方不太能更彻底的了解漏洞在哪里,那么try和catch机制来最大化地挽留损失,以上程序中,try将可能发生错误的地方抛出,当执行发生错误了,计算机并不报错,而是有catch语句抓住,执行其中内容,后面还有个finally语句,用于善后的代码,不管是否有异常发生,finally语句块中的语句始终保证被执行.

    二:例题:

    package test;
    
    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(紧跟着的catch)接收一并执行,外层的try抛出异常二,对应的catch接收二并执行,抛出异常和接收异常这执行可以看成是一个单一的动作操作,最后的catch就没有抛出这一动作执行,所以不运行(因为抛出异常一已被接住)。

    看另一个例题:

    package test;
    
    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

    分析:

    此例题和上面例题的区别就在于,里面的catch所接住的方向变了。

    按着程序顺序分析下来,当里面的try抛出异常时,只有外面的catch能接住,那么开始执行外面的catch,顺序就从刚才执行的语句之下执行下去了,即便是外面的catch交换顺序,结果一样不变。

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

    三:例题:

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

    结果:

    in Level 1
    in Level 2
    in Level 3
    Level 3:class java.lang.ArithmeticException
    In Level 3 finally
    In Level 2 finally
    In Level 1 finally

    总结:finally是无论是否出现异常都会执行的,在第三个try中出现异常,紧跟着的catch已经接收到,但此并不算第二个try出现异常,因为异常已经解决,那么之后就不会显示第二个和第一个catch的内容了。我将内容改了一下,就验证了我的说法。

    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 (ArrayIndexOutOfBoundsException e) {  //我让此catch不能捕捉到上面的try,有意让第二个catch抓到
                        System.out.println("Level 3:" + e.getClass().toString());
                    }
                    finally {   
                        System.out.println("In Level 3 finally");
                    }
                    // result=100/0;  //Level 2
                    }
                catch (Exception e) {//第二个catch
                     System.out.println("Level 2:" + e.getClass().toString());
                 }
                 finally {
                    System.out.println("In Level 2 finally");
                 }


    结果显示

    in Level 1
    in Level 2
    in Level 3
    In Level 3 finally
    Level 2:class java.lang.ArithmeticException
    In Level 2 finally
    In Level 1 finally

    结论:try catch着一单一动作(前提是配套),就是出现异常和处理异常的程序,那总的来看,依旧是无异常的代码。

    四:例题:

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


    结果:

    in main
    Exception is thrown in main

    总结:

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

    PS:System.exit(status)不管status为何值都会退出程序,非0,则表示为非正常退出程序,一般放在catch块中,当捕获到异常,需要停止程序,用非0值来表示非正常退出程序

  • 相关阅读:
    测试一个纸杯需要考虑什么?
    第三十三天-rsync高级同步工具深度实战
    运维人员必须熟悉的运维工具汇总
    Mysql 高负载排查思路
    查看mysql主从配置的状态及修正 slave不启动问题
    第三十二天-rsync高级同步工具基础
    第三十一天-两个例题
    linux mail 命令参数
    第三十天-ssh key企业批量分发自动化管理案例
    第二十九天-ssh服务重要知识深入浅出讲解
  • 原文地址:https://www.cnblogs.com/maplely/p/4966270.html
Copyright © 2011-2022 走看看