zoukankan      html  css  js  c++  java
  • 动手动脑(五)

    一,

     1 import javax.swing.*;
     2 
     3 class AboutException {
     4    public static void main(String[] a) 
     5    {
     6       int i=1, j=0, k;
     7       k=i/j;
     8 
     9 
    10     try
    11     {
    12         
    13 k = i/j; // Causes division-by-zero exception 14 //throw new Exception("Hello.Exception!"); 15 } 16 17 catch ( ArithmeticException e) 18 { 19 System.out.println("被0除. "+ e.getMessage()); 20 } 21 22 catch (Exception e) 23 { 24 if (e instanceof ArithmeticException) 25 System.out.println("被0除"); 26 else 27 { 28 System.out.println(e.getMessage()); 29 30 } 31 } 32 33 34 finally 35 { 36 JOptionPane.showConfirmDialog(null,"OK"); 37 } 38 39 } 40 }

    程序运行截图:

     1 import javax.swing.*;
     2 
     3 class AboutException {
     4    public static void main(String[] a) 
     5    {
     6       //int i=1, j=0, k;
     7       //k=i/j;
     8 
     9 
    10     try
    11     {
    12         int i=1, j=0, k;
    13         k = i/j;    // Causes division-by-zero exception
    14         //throw new Exception("Hello.Exception!");
    15     }
    16     
    17     catch ( ArithmeticException e)
    18     {
    19         System.out.println("被0除.  "+ e.getMessage());
    20     }
    21     
    22     catch (Exception e)
    23     {
    24         if (e instanceof ArithmeticException)
    25             System.out.println("被0除");
    26         else
    27         {  
    28             System.out.println(e.getMessage());
    29             
    30         }
    31     }
    32 
    33     
    34     finally
    35      {
    36              JOptionPane.showConfirmDialog(null,"OK");
    37      }
    38         
    39   }
    40 }

    程序运行截图:

    程序分析:

    try语句中运行发生异常,跳转到catch语句中执行System.out.println("被0除.  "+ e.getMessage());语句,所以输出如上。无论是否发生异常最后执行了finally语句,弹出上述页面防止程序崩溃。

    二,

     1 public class CatchWho { 
     2     public static void main(String[] args) { 
     3         try { 
     4                 try { 
     5                     throw new ArrayIndexOutOfBoundsException(); 
     6                 } 
     7                 catch(ArrayIndexOutOfBoundsException e) { 
     8                        System.out.println(  "ArrayIndexOutOfBoundsException" +  "/内层try-catch"); 
     9                 }
    10  
    11             throw new ArithmeticException(); 
    12         } 
    13         catch(ArithmeticException e) { 
    14             System.out.println("发生ArithmeticException"); 
    15         } 
    16         catch(ArrayIndexOutOfBoundsException e) { 
    17            System.out.println(  "ArrayIndexOutOfBoundsException" + "/外层try-catch"); 
    18         } 
    19     } 
    20 }

    运行程序截图:

    运行后思考:

    我们根据程序运行的结果可以知道程序实际上式执行了第8行与第14行的输出语句,而没有执行第17行。

     1 public class CatchWho2 { 
     2     public static void main(String[] args) { 
     3         try {
     4                 try { 
     5                     throw new ArrayIndexOutOfBoundsException(); 
     6                 } 
     7                 catch(ArithmeticException e) { 
     8                     System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch"); 
     9                 }
    10             throw new ArithmeticException(); 
    11         } 
    12         catch(ArithmeticException e) { 
    13             System.out.println("发生ArithmeticException"); 
    14         } 
    15         catch(ArrayIndexOutOfBoundsException e) { 
    16             System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch"); 
    17         } 
    18     } 
    19 }

    运行截图:

    运行后截图:

    我们根据程序运行的结果可以知道程序实际上式执行了第16行的输出语句,而没有执行第8行与第13行。

    总结分析:

    通过上述两个实例,我们可以总结出java多层嵌套异常处理的基本流程:

    try语句可以被嵌套。也就是说,一个try语句可以在另一个try块内部。每次进入try语句,异常的前后关系都会被推入堆栈。如果一个内部的try语句不含特殊异常的catch处理程序,堆栈将弹出,下一个try语句的catch处理程序将检查是否与之匹配。这个过程将继续直到一个catch语句匹配成功,或者是直到所有的嵌套try语句被检查耗尽。如果没有catch语句匹配,Java的运行时系统将处理这个异常。

     三,

     1 public class EmbededFinally {
     2 
     3     
     4     public static void main(String args[]) {
     5         
     6         int result;
     7         
     8         try {
     9             
    10             System.out.println("in Level 1");
    11 
    12            
    13              try {
    14                 
    15                 System.out.println("in Level 2");
    16   // result=100/0;  //Level 2
    17                
    18                  try {
    19                    
    20                      System.out.println("in Level 3");
    21                       
    22                      result=100/0;  //Level 3
    23                 
    24                 } 
    25                 
    26                 catch (Exception e) {
    27                     
    28                     System.out.println("Level 3:" + e.getClass().toString());
    29                 
    30                 }
    31                 
    32                 
    33                 finally {
    34                     
    35                     System.out.println("In Level 3 finally");
    36                 
    37                 }
    38                 
    39                
    40                 // result=100/0;  //Level 2
    41 
    42             
    43                 }
    44             
    45             catch (Exception e) {
    46                
    47                  System.out.println("Level 2:" + e.getClass().toString());
    48            
    49              }
    50              finally {
    51                 
    52                 System.out.println("In Level 2 finally");
    53            
    54              }
    55              
    56             // result = 100 / 0;  //level 1
    57         
    58         } 
    59         
    60         catch (Exception e) {
    61             
    62             System.out.println("Level 1:" + e.getClass().toString());
    63         
    64         }
    65         
    66         finally {
    67            
    68 .             System.out.println("In Level 1 finally");
    69         
    70         }
    71     
    72     }
    73 
    74 }

    运行截图:

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

    四,

     1 public class SystemExitAndFinally {
     2 
     3     
     4     public static void main(String[] args)
     5     {
     6         
     7         try{
     8 
     9             
    10             System.out.println("in main");
    11             
    12             throw new Exception("Exception is thrown in main");
    13 
    14                     //System.exit(0);
    15 
    16         
    17         }
    18         
    19         catch(Exception e)
    20 
    21             {
    22             
    23             System.out.println(e.getMessage());
    24             
    25             System.exit(0);
    26         
    27         }
    28         
    29         finally
    30         
    31         {
    32             
    33             System.out.println("in finally");
    34         
    35         }
    36     
    37     }
    38 
    39 
    40 }

    运行截图:

    实验总结:

    如果finally块中的代码过多会导致字节码条数”膨胀”,因为finally中的字节码会被”复制”到try块和所有的catch块中,出异常了。System.out.println("F")不在finally里。而只有finally里的语句才会不论如何都会执行。

  • 相关阅读:
    redis基础
    docker日志清理脚本
    Hive修改表的所有者(Owner)
    User xxx does not have privileges for CREATETABLE的解决办法
    Spark读取Hive表中文显示乱码的解决办法
    Go语言之标识符与关键字
    Go语言之数据类型(三)
    bootstrapTable频繁向后台接口发请求
    框架整合疑云
    业务开发中-设计模式使用
  • 原文地址:https://www.cnblogs.com/mawangwang/p/9943605.html
Copyright © 2011-2022 走看看