zoukankan      html  css  js  c++  java
  • 2020.10.27

    一、今日学习内容

        动手动脑

    1、阅读代码,写出运行结果

    复制代码
     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 }
    复制代码

    【运行结果】

      ArrayIndexOutOfBoundsException/内层try-catch
      发生ArithmeticException

    2、写出运行结果

    复制代码
     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 }
    复制代码

    【运行结果】

    ArrayIndexOutOfBoundsException/外层try-catch

    3、

    复制代码
     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 }
    复制代码

    【运行结果】

      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

    【总结】由此得出,代码运行类似于栈,先运行的后捕获,后抛出的先捕获

    4、

    复制代码
    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

    【结论】finally语句不一定被执行,当捕获错误处理完成并退出后便不再执行finally语句。

    5、课程感受

    通过这次课程,了解了Java的异常处理机制,可以有多个catch语句块,每个代码块捕获一种异常。在某个try块后有两个不同的catch 块捕获两个相同类型的异常是语法错误。使用catch语句,只能捕获Exception类及其子类的对象。因此,一个捕获Exception对象的catch语句块可以捕获所有“可捕获”的异常。将catch(Exception e)放在别的catch块前面会使这些catch块都不执行,因此Java不会编译这个程序。

    二、遇到的问题

        对于很多异常情况不知道,不会用

    三、明日计划

       明天总结Java项目中常见的异常情况

  • 相关阅读:
    V-Box
    One CLI for webpack must be installed. These are recommended choices, delivered as separate packages:
    获取用户地理位置
    错误 CS0234 命名空间“Microsoft”中不存在类型或命名空间名“Reporting”(是否缺少程序集引用?)
    “/Reports”应用程序中的服务器错误。
    微信公众号关注界面的菜单栏跟实际的菜单栏不一致
    错误 CS8107 C# 7.0 中不支持功能“xxxxxx”。请使用 7.1 或更高的语言版本。
    已经安装了 AccessDatabaseEngine.exe,还是报错
    没有足够的内存继续执行程序(mscorlib)
    坑,坑,
  • 原文地址:https://www.cnblogs.com/wmdww/p/14149978.html
Copyright © 2011-2022 走看看