• Java的动手动脑(六)


     日期:2018.11.8

    星期四

    博客期:022

    -----------------------------------------------------------------------------------------

      Part 1: 基本异常处理

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

      运行结果如下:

      说明:因为当你的程序出现错误的时候,即第一个 k = i / j ;语句执行的时候,你的程序运行到这里就结束(中断)了,不可能继续运行try{}里的 k = i / j ;语句,所以会是这个结果!而当你将它的第一个 k = i / j; 用//或/**/注释掉之后 ,运行结果如下:

      在你注释掉了以后,它会继续执行try{}里的 k = i / j ;语句,也就会在try{}里抛出 ArithmeticException ,而后边刚好有catch 语句接到了,进而做了内部处理,由于 ArithmeticException 已经得到了 catch,后面的 catch (Exception e) 就没有接到,于是不执行,而后面的finally除了特殊情况,一般是要执行的,于是便是这样的结果!但如果你把第二个 k = i / j; 也用//或/**/注释掉之后,运行结果如下:

      理由很清晰,就是根据类型执行catch 语句和 finally 语句!

    -----------------------------------------------------------------------------------------

      Part 2: 多层的异常捕获(1+2)

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

      这两段代码的运行截图分别如下:

      说明:那我就把这两个案例综合在一起分析吧!首先对比这两个案例!我们可以看到区别,内部try{}里的所要catch的异常不同——ArrayIndexOutOfBoundsException和ArithmeticException,但究竟是什么原因导致了这结果的不同呢?我来说吧!这无外乎就是这一个catch的嵌套比较特殊!因为它两层的嵌套有重复的Exception监听处理,这就产生了另外一个问题——究竟是哪个或哪些部分catch到了异常呢?我们从运行结果中可以得知,catch是就近原则的,于是只要这个 Exception 被 catch 到了就不能再被非finally的catch语句catch到!所以就是这样的结果了!因为Exception不属于内部的catch的要求类型,因而没有被内部的catch语句catch到,而是外部的catch语句!所以第二个就会执行外部的catch操作!

    -----------------------------------------------------------------------------------------

      Part 3: 不同的finally语句的顺序

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

      运行结果:

      说明:这个问题恰恰说的就是我Part 2所提到的问题——那个监听类型重复之后的顺序问题!就拿本程序来说,输出的顺序毫无疑问是1、2、3的顺序,而后面的finally的执行顺序却是3、2、1的顺序,当然这也是很容易理解的——毕竟还是按顺序来说,先执行try内部,当这个try的finally{}结束之后,外部的finally{}才能得以继续!所以finally的执行顺序是从内到外的!

    -----------------------------------------------------------------------------------------

      Part 4: 解析finally不执行的特殊情况

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

      运行结果:

      说明:这个问题说明即使是finally也不能改变System.exit(0);能够直接退出运行状态的功能!这可能是finally唯一的不执行的情况!嗯,就目前来看吧,嗯!好像利用关机、任务管理器直接退出eclipse的,我就不说了!

    -----------------------------------------------------------------------------------------

  • 相关阅读:
    <c:forEach>详解
    JSP基本_JSTL
    鼠标显示效果的形状设置
    linux7.3+nginx1.1+tomcat8.5 搭建负载均衡
    安装zabbix
    Centos7 systemctl使用
    Centos7 yum安装 Lnmp以及Lamp
    Centos7 LAMP环境下安装zabbix3.0
    centos 7.0 搭建LAMP环境
    mysql 配置参数详解
  • 原文地址:https://www.cnblogs.com/onepersonwholive/p/9930040.html
Copyright © 2020-2023  润新知