• 第四次动手动脑


    (一)

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

    运行结果:

    Exception in thread "main" java.lang.ArithmeticException: / by zero
    at lianxi/JavaApp1.AboutException.main(AboutException.java:9)

    (二)

    package JavaApp1;
    
    import javax.swing.*;
    
    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

    最后一个没有抛出异常,所以没有输出。

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

    (三)

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

    try嵌套依次执行,由于只有第三个中存在异常,所以捕获到了异常,finlly语句全部依次输出。

    (四)

    package JavaApp;
    
    
    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语句不一定会执行。

  • 相关阅读:
    二维码
    文件下载
    相对路径使用的特殊符号
    Httpclient的使用
    MySQL比like语句更高效的写法
    jQuery中turn.js(翻页效果)学习笔记
    如何在忘记mysql的登录密码时更改mysql登录的密码(window及linux)
    详细介绍svn在eclipse中的使用(附图解说明)
    Xshell6远程访问linux及Xftp6远程针对linux系统中文件操作(附图文详解)
    利用workbench对linux/Ubuntu系统中的mysql数据库进行操作
  • 原文地址:https://www.cnblogs.com/liyuchao/p/9942053.html
Copyright © 2020-2023  润新知