質問

Here is the code to demonstrate my point (java):

public static int getSize(List<String> list)
{
    System.out.println("begin");
    try
    {
        System.out.println("get list size");
        return list.size();
    }
    catch (Exception e)
    {
        System.err.println("exception");
    }
    finally
    {
        System.out.println("finally");
        return -1;
    }
}

public static void main(String[] args) 
{
    List<String> list = null;
    int size = getSize(list);
    System.out.println("list size: " + size);
}

I expected the output of:

begin
get list size
exception
finally
list size: -1

instead I get other stuff and the one above as well... like the exception can show before get list size and after exception...

e.g.

begin
exception
get list size
finally
list size: -1

and

begin
get list size
finally
exception
list size: -1

Why is it doing this? What is the correct output?

役に立ちましたか?

解決

System.out and System.err are differents stream. The test is displayed when the strem flush. As they are differents, you can't be sure of when each flush will happen. Try to display the exception in System.out and see the difference.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top