zoukankan      html  css  js  c++  java
  • 07-异常处理(动手动脑问题)

    一、.异常处理基础

    测试代码一:

     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 }

    运行结果截图:

    因执行 i/j 零作为除数而抛出异常

    测试代码二(使用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 }

    执行结果:

    JAVA异常处理:

    1.捕获语句:

    Try{                                          

                  //可能发生运行错误的代码;
    }                                              
          catch(异常类型 异常对象引用){
         //用于处理异常的代码
    }                                             
    finally{                                     
     //用于“善后” 的代码
    }                                             

     2.异常处理机制:

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

    二、多层异常捕获:

    测试代码一:

     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 }

    执行结果:

    测试代码二:

     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 }

    执行结果:

    原因分析:

    JAVA异常处理流程:

    (1)如果程序中产生了异常,那么JVM根据异常的类型,实例化一个指定异常类的对象;

    (2)如果这时程序中没有任何的异常处理操作,则这个异常类的实例化对象将交给JVM进行处理,而JVM的默认处理方式就是进行异常信息的输出,而后中断程序执行;

    (3)如果程序中存在了异常处理,则会由try语句捕获产生的异常类对象;

    (4)与try之后的每一个catch进行匹配,如果匹配成功,则使用指定的catch进行处理,如果没有匹配成功,则向后面的catch继续匹配,如果没有任何的catch匹配成功,则这个时候将交给JVM执行默认处理;

    (5)不管是否有异常都会执行finally程序,如果此时没有异常,执行完finally,则会继续执行程序之中的其他代码,如果此时有异常没有能够处理(没有一个catch可以满足),那么也会执行finally,但是执行完finally之后,将默认交给JVM进行异常的信息输出,并且程序中断。

    (参考:http://blog.csdn.net/wei_zhi/article/details/52836838

     三、finally执行时机:

    测试代码:

     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 代码块出现在 catch 代码块最后,用来创建在 try 代码块后面执行的代码块。

    无论是否发生异常,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内的语句将不再执行。

     五、课程成绩:

    程序代码:

     1 import java.util.Scanner;
     2 
     3 public class Grade {
     4     public static void main(String[] args) {
     5         // TODO Auto-generated method stub
     6         double score = 0;
     7         String G;
     8         while(true) {
     9             Scanner in = new Scanner(System.in);
    10             try {
    11                 score = in.nextDouble();
    12                 int s = (int)score;
    13                 s = s / 10;
    14                 switch(s) {
    15                 case 10:
    16                 case 9:G = "优";break;
    17                 case 8:G = "良";break;
    18                 case 7:G = "中";break;
    19                 case 6:G = "及格";break;
    20                 case 5:
    21                 case 4:
    22                 case 3:
    23                 case 2:
    24                 case 1:
    25                 case 0:G = "不及格";break;
    26                 default:throw new Exception();
    27                 }
    28                 System.out.println(G);
    29                 break;
    30             }catch(Exception e) {
    31                 System.out.println("您输入的数据有误,请重新输入");
    32             }
    33         }
    34     }
    35 }

    执行结果:

  • 相关阅读:
    vue播放mu38视频兼容谷歌ie等浏览器
    cube-slide组件的应用
    cube-ui按钮配合toast单例模式应用
    vue网页添加水印
    element ui 下拉框绑定对象并且change传多个参数
    VUE 同一页面路由参数变化,视图不刷新的解决方案
    MySQL Out-of-Band 攻击
    mysql load_file在数据库注入中使用
    Handy Collaborator :用于挖掘out-of-band类漏洞的Burp插件介绍
    基于时间的 SQL注入研究
  • 原文地址:https://www.cnblogs.com/lzq666/p/7886361.html
Copyright © 2011-2022 走看看