Unreachable statement when using return in finally?

Posted by abson on Stack Overflow See other posts from Stack Overflow or by abson
Published on 2010-03-23T17:38:18Z Indexed on 2010/03/23 18:23 UTC
Read the original article Hit count: 382

Filed under:
|

this compiles

class ex1
{
    public int show()
    {
        try
        {
            int a=10/10;
            return 10;
        }
        catch(ArithmeticException e)
        {
            System.out.println(e);
        }
        finally
        {
            System.out.println("Finally");
        }

        System.out.println("hello");
        return 20;
    }
}

on the other hand this doesn't

class ex15
{
    public int show()
    {
        try
        {
            int a=10/0;
            return 10;
        }
        catch(ArithmeticException e)
        {
            System.out.println(e);
        }
        finally
        {
            System.out.println("Finally");
            return 40;
        }

        System.out.println("hello");
        return 20;
    }
}

and gives unreachable statement System.out.println("hello"); error. why is it so?

© Stack Overflow or respective owner

Related posts about beginner

Related posts about java