try-finally block continue for loop

2019-09-10 00:46发布

I'm writing a loop that ignored the Exception and it works well.

    for (; flag; ) {
        try {
            //do something ignore exception.
            Runnable r = queue.pollFirst();
            r.run();
        } catch (Exception ignored) {
            // ignored.
        }
    }

But my question is: If I don't catch RuntimeException and force continue loop in finally block, what will happen to the Exception and returned value?

Example:

    for (int i = 0; i < 10; i++) {
        try {
            System.out.println(i);

            throw new RuntimeException();
        } finally {
            //what will happen to the exception if continue loop?
            continue;
        }
    }

3条回答
▲ chillily
2楼-- · 2019-09-10 00:58

Runtime Exception will be ignored because there is no catch block to access/use (e.g. for logging purpose) thrown object of java.lang.RuntimeException. finally block does not have any access to Exception object thrown by try block. Its better to have catch block to get more information.

查看更多
Evening l夕情丶
3楼-- · 2019-09-10 01:06

Not sure why you would want to catch a RuntimeException because by the time you even try catching it, it's too late therefore your continue will never hit.

查看更多
手持菜刀,她持情操
4楼-- · 2019-09-10 01:07

They will be ignored as the finally block has the final word.

查看更多
登录 后发表回答