动手动脑(异常处理)

1.请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解java中实现异常处理的基本知识

源代码

import javax.swing.*;
public 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");
     }
        
  }
}

实验结果截图

2.请尝试解释以下奇怪现象

jvm在处理浮点数时,生成的是ddiv字节码指令,i/0,0转化为浮点数0.0,而0.0是double类型的,并不精确,所以不会抛出异常。

jvm在处理整数时,生成的是idiv字节码指令,整数除0就是除0,会抛出异常。

3.阅读以下代码(CatchWho.java),写出程序运行结果:

源代码

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

结果截图

扫描二维码关注公众号,回复: 4009448 查看本文章

4.写出CatchWho2.java程序运行的结果

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

结果截图

try catch这一模式,是有顺序依据的,当执行try语句是,紧接着的就是所对应的catch来执行,然后接着catch继续执行下去。

5.请先阅读 EmbedFinally.java示例,再运行它,观察其输出并进行总结。

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

}

结果截图

总结:

  finally是无论是否出现异常都会执行的,在第三个try中出现异常,紧跟着的catch已经接收到,但此并不算第二个try出现异常,因为异常已经解决,那么之后就不会显示第二个和第一个catch的内容了。

6.辨析:finally语句块一定会执行吗?

请通过 SystemExitAndFinally.java示例程序回答上述问题

源程序

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


}

结果截图:

JVM是java虚拟机,finally是由JVM保证执行,而System.exit(0)是正常退出程序,结束JVM的运行,那么最后finally就不再执行。

    finally语句不被执行的唯一情况是先执行了用于终止程序的System.exit()方法。

猜你喜欢

转载自www.cnblogs.com/liujinxin123/p/9938671.html