Обработка исключений в Java - PullRequest
0 голосов
/ 01 сентября 2010

У меня есть вопрос о пробовать, ловить и, наконец, в Java.Рассмотрим следующий сценарий:

try{
//Some code here that throws IOExceotion
} 
catch (IOException ex){
System.out.println("Line 1: IOException Encountered!");
throw ex;
}
finally {
System.out.println("Line 2: I am always executed!");
}

Каким будет вывод приведенного выше фрагмента кода?Я собираюсь посмотреть:

Line 1: IOException Encountered!
Line 2: I am always executed!

или это будет

Line 2: I am always executed!
Line 1: IOException Encountered!

Или это будет просто (поскольку у нас есть бросок в блоке catch)

Line 1: IOException Encountered!

В принципе, я не нашел пример, где есть "throw" в блоке catch и, наконец, блок после блока catch (как в примере выше).Кто-нибудь может пролить свет на это?

Спасибо.

Ответы [ 3 ]

5 голосов
/ 01 сентября 2010

Цитирование Спецификации языка Java, §14.20.4 :

Оператор try с блоком finally выполняется при первом выполнении блока try.Тогда есть выбор:

* If execution of the try block completes normally, then the finally block is executed, and then there is a choice:
      o If the finally block completes normally, then the try statement completes normally.
      o If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S. 
* If execution of the try block completes abruptly because of a throw of a value V, then there is a choice:
      o If the run-time type of V is assignable to the parameter of any catch clause of the try statement, then the first (leftmost) such catch clause is selected. The value V is assigned to the parameter of the selected catch clause, and the Block of that catch clause is executed. Then there is a choice:
            + If the catch block completes normally, then the finally block is executed. Then there is a choice:
                  # If the finally block completes normally, then the try statement completes normally.
                  # If the finally block completes abruptly for any reason, then the try statement completes abruptly for the same reason. 
            + If the catch block completes abruptly for reason R, then the finally block is executed. Then there is a choice:
                  # If the finally block completes normally, then the try statement completes abruptly for reason R.
                  # If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and reason R is discarded). 
      o If the run-time type of V is not assignable to the parameter of any catch clause of the try statement, then the finally block is executed. Then there is a choice:
            + If the finally block completes normally, then the try statement completes abruptly because of a throw of the value V.
            + If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and the throw of value V is discarded and forgotten). 
* If execution of the try block completes abruptly for any other reason R, then the finally block is executed. Then there is a choice:
      o If the finally block completes normally, then the try statement completes abruptly for reason R.
      o If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and reason R is discarded). 
3 голосов
/ 01 сентября 2010

Вы увидите первый.Наконец, блок выполняется всегда и как последний.

2 голосов
/ 01 сентября 2010

Это поп-викторина? Просто шучу. Вы увидите первый, если есть исключение. Оператор finally всегда будет выполняться, поэтому он будет напечатан всегда.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...