07-异常处理——动手动脑

1.请阅读并运行AboutException.java示例,了解java中实现异常处理的基础知识。

AboutException.java源代码:

package test6;

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

程序运行结果:

基础知识:

1、把可能会发生错误的代码放进try语句当中。

2、当程序检测到出现了一个错误时会抛出一个异常对象。异常处理代码会捕获并处理这个错误。

                   catch语句块中的代码用于处理错误。

3、当发生异常时,程序控制流程由try语句块跳转到catch语句块。

4、不管是否有异常发生,finally语句块中的语句始终保证被执行。

5、如果没有提供合适的异常处理代码,JVM将会结束掉整个应用程序。

6、java中所有可捕获的异常都派生自Exception类。

2.

 

原因:利用javap反编译后发现,JVM在具体实现这两个指令时,采用了不同的处理策略,导致两段代码运行时得到不同的结果。

3.多层的异常捕获1

阅读以下代码,写出程序运行结果:

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

程序运行结果:

 

4.多层的异常捕获2

阅读以下代码,写出程序运行结果:

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

4.请先阅读 EmbedFinally.java示例,再运行它,观察其输出并进行总结。finally语句一定会执行吗?

特别注意:

当有多层嵌套的finally时,异常在不同的层次抛出,在不同的位置抛出,可能会导致不同的finally语句块执行顺序。

(1)、只有与 finally 相对应的 try 语句块得到执行的情况下,finally 语句块才可能会执行。

(2)、在 try 语句块中执行了 System.exit (0) 语句,终止了 Java 虚拟机的运行时。

(3)、不管 try 语句块正常结束还是异常结束,finally 语句块是保证要执行的。如果 try 中有控制转移语句(return、break、continue)finally 语句块在 try 或catch语句块中的 return 语句之前执行。

posted @ 2017-11-16 22:44  编程小大白  阅读(180)  评论(0编辑  收藏  举报