异常丢失

Author Avatar
罗炜光 3月 11, 2016
  • 在其它设备中阅读本文章

异常丢失指的是在finally的代码块里有return或throw语句时,此语句会覆盖前面的return语句或throw语句,但在eclipse中,在finally的代码块加入return或throw语句时,会报“finally block does not complete normally”警告,所以稍微知道一下就好了。

代码:

public class TestException 
{  

    void Test1()
    {
        try
        {
            if(Test1Test())
            {
                throw new Exception();
            }
        } catch (Exception e)
        {
            System.out.println("Test1,catch");
        }
        finally
        {
            System.out.println("Test1,finally");
        }
    }

    boolean Test1Test() 
    {
        try
        {
            throw new Exception();
        } catch (Exception e)
        {
            System.out.println("Test1Test,catch");
            return true;
        }
        finally
        {
            System.out.println("Test1Test,finally");

        }



    }



    void Test2()
    {
        try
        {
            if(Test2Test())
            {
                throw new Exception();
            }
        } catch (Exception e)
        {
            System.out.println("Test2,catch");
        }
        finally
        {
            System.out.println("Test2,finally");
        }
    }

    boolean Test2Test() 
    {
        try
        {
            throw new Exception();
        } catch (Exception e)
        {
            System.out.println("Test2Test,catch");
        }
        finally
        {
            System.out.println("Test2Test,finally");
            return false;
        }



    }


    void Test3()
    {
        try
        {
            if(Test3Test())
            {
                throw new Exception();
            }
        } catch (Exception e)
        {
            System.out.println("Test3,catch");
        }
        finally
        {
            System.out.println("Test3,finally");
        }
    }

    boolean Test3Test() throws Exception
    {
        try
        {
            throw new Exception();
        } catch (Exception e)
        {
            System.out.println("Test3Test,catch");
            return true;
        }
        finally
        {
            System.out.println("Test3Test,finally");
            return false;
        }



    }


    void Test4()
    {
        try
        {
            if(Test4Test())
            {
                throw new Exception();
            }
        } catch (Exception e)
        {
            System.out.println("Test4,catch");
        }
        finally
        {
            System.out.println("Test4,finally");
        }
    }

    boolean Test4Test() throws Exception
    {
        try
        {
            throw new Exception();
        } catch (Exception e)
        {
            System.out.println("Test4Test,catch");
            throw new Exception("aaaTest4Test,catch");
        }
        finally
        {
            System.out.println("Test1Test,finally");
            return false;
        }



    }

    void Test5()
    {
        try
        {
            if(Test5Test())
            {
                throw new Exception();
            }
        } catch (Exception e)
        {
            System.out.println("Test5,catch");
            System.out.println(e.toString());
        }
        finally
        {
            System.out.println("Test5,finally");
        }
    }

    boolean Test5Test() throws Exception
    {
        try
        {
            throw new Exception();
        } catch (Exception e)
        {
            System.out.println("Test5Test,catch");
            return false;
        }
        finally
        {
            System.out.println("Test5Test,finally");
            throw new Exception("bbbTest5Test,catch");
        }



    }





    void Test6()
    {
        try
        {
            if(Test6Test())
            {
                throw new Exception();
            }
        } catch (Exception e)
        {
            System.out.println("Test6,catch");
            System.out.println(e.toString());
        }
        finally
        {
            System.out.println("Test6,finally");
        }
    }

    boolean Test6Test() throws Exception
    {
        try
        {
            throw new Exception();
        } catch (Exception e)
        {
            System.out.println("Test6Test,catch");
            throw new Exception("aaaTest6Test,catch");
        }
        finally
        {
            System.out.println("Test1Test,finally");
            throw new Exception("bbbTest1Test,catch");
        }



    }

    public static void main(String[] args) 
    {   
        System.out.println("--------------------------------------------------");
        new TestException().Test1();
        System.out.println("--------------------------------------------------");
        new TestException().Test2();
        System.out.println("--------------------------------------------------");
        new TestException().Test3();
        System.out.println("--------------------------------------------------");
        new TestException().Test4();
        System.out.println("--------------------------------------------------");
        new TestException().Test5();
        System.out.println("--------------------------------------------------");
        new TestException().Test6();
    }  
}

运行结果

--------------------------------------------------
Test1Test,catch
Test1Test,finally
Test1,catch
Test1,finally
--------------------------------------------------
Test2Test,catch
Test2Test,finally
Test2,finally
--------------------------------------------------
Test3Test,catch
Test3Test,finally
Test3,finally
--------------------------------------------------
Test4Test,catch
Test1Test,finally
Test4,finally
--------------------------------------------------
Test5Test,catch
Test5Test,finally
Test5,catch
java.lang.Exception: bbbTest5Test,catch
Test5,finally
--------------------------------------------------
Test6Test,catch
Test1Test,finally
Test6,catch
java.lang.Exception: bbbTest1Test,catch
Test6,finally