动手动脑5

动手动脑1:

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

运行结果:

未使用异常处理的运行结果结果

添加try…catch和合适的异常处理代码后之后

 

结论:异常发生,运行catch中的异常处理代码,最后执行finally中的内容(不管是否有异常发生,finally语句块中的语句始终保证被执行)。

技术探索:

前页PPT中所出现之奇怪现象,可以使用javap去反汇编两个示例程序的.class文件(一个是AboutException.class,另一个是ThrowDemo.class),从中你会有所发现的。

反汇编:

 

动手动脑2:

阅读以下代码(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");

        }

    }

}

运行结果:

写出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");

        }

    }

}

运行结果:

动手动脑3:

请先阅读 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");

       

        }

   

    }

 

}

运行结果:

结论:嵌套使用try…catch语句时,最内层的finally会先执行,之后由内而外的执行。

动手动脑4:

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

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

代码:

复制代码
 1 public class SystemExitAndFinally 
 2 {
 3 
 4     
 5     public static void main(String[] args)
 6     
 7     {
 8         
 9         try
10         {
11 
12             
13             System.out.println("in main");
14             
15             throw new Exception("Exception is thrown in main");
16 
17                     //System.exit(0);
18 
19         
20         }
21         
22         catch(Exception e)
23 
24             {
25             
26             System.out.println(e.getMessage());
27             
28             System.exit(0);
29         
30         }
31         
32         finally
33         
34         {
35             
36             System.out.println("in finally");
37         
38         }
39     
40     }
41 
42 
43 }
复制代码

运行结果:

结论:

在catch中使用System.exit(0);程序会提前结束不会执行finally语句。

原文地址:https://www.cnblogs.com/chenghaixiang/p/14158467.html