Tami Wakana wrote:The question asks what does the output of the code contain and the answer is "abce" and "An exception with the message set to "3"". I understand the exception with the message set to "3", but I don't understand why the output is just abce. Shouldn't it include "d" since line 8 throws a RuntimeException and there is a catch block that takes in a RuntimeException?
No, the output of that code snippet is actually "abce"
and an uncaught RuntimeException
with a message set to "3"
And the reason for this is very simple: the catch
blocks/handlers only catch possible exceptions thrown from within the try
block. The RuntimeException
is not thrown from the try
block but from within a catch
block/handler. And that's why this exception is not caught by the catch
block/handler of RuntimeException
Hope it helps!