Почему второй блок catch опускается? (Java SE 8 OCA — исключения)JAVA

Программисты JAVA общаются здесь
Ответить
Гость
 Почему второй блок catch опускается? (Java SE 8 OCA — исключения)

Сообщение Гость »


Я пытался решить этот пример вопроса для экзамена OCA Java SE 8, но ошибся.

Код: Выделить всё

public static void main(String... a) {
System.out.print("a");
try {
System.out.print("b");
throw new IllegalArgumentException("1");
} catch (IllegalArgumentException e) {
System.out.println("c");
throw new RuntimeException("1");
} catch (RuntimeException e) {
System.out.println("d");
throw new RuntimeException("2");
} finally {
System.out.print("e");
throw new RuntimeException("3");
}
}
Here is the image of the try-catch sequence in question
I believe the answer should be:

Код: Выделить всё

abcdeException in thread "main" java.lang.RuntimeException: 3
but the answer is actually:

Код: Выделить всё

abceException in thread "main" java.lang.RuntimeException: 3
My thought process is that first, the main method prints "a", then prints "b". Then, an

Код: Выделить всё

IllegalArgumentException
is thrown, and caught in the first catch block. Then, inside the first catch block, "c" is printed, and a

Код: Выделить всё

RuntimeException
is thrown. I really do believe, then, that the second catch block catches the

Код: Выделить всё

RuntimeException
and prints "d", while throwing another

Код: Выделить всё

RuntimeException
, where the thread then "floats" over to the finally block, printing "e" and then throwing the

Код: Выделить всё

RuntimeException("3")
instead. I really can't see why the second catch block is simply ignored.
Therefore,

Код: Выделить всё

abcdeException in thread "main" java.lang.RuntimeException: 3
should be printed in my opinion.
Am I missing anything in terms of reasoning? How should my sequence of thoughts be instead?


Источник: https://stackoverflow.com/questions/781 ... exceptions
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»