2010-08-31 6 views
0

J'ai une question à propos de try, catch et enfin en Java. Considérez le scénario suivant:Gestion des exceptions en 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!"); 
} 

Quelle serait la sortie de l'extrait de code ci-dessus? Est-ce que je vais voir:

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

ou serait-il

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

Ou serait-il juste (puisque nous avons un jet dans le bloc catch)

Line 1: IOException Encountered! 

Fondamentalement, Je n'ai pas trouvé d'exemple où il y a un "lancer" dans le bloc catch et finalement bloquer après le bloc catch (comme dans l'exemple ci-dessus). Quelqu'un peut-il nous éclairer?

Merci.

+6

Dans le temps qu'il vous a fallu pour écrire votre question, vous pourriez avoir essayé pour vous-même, sûrement. – skaffman

+0

Sonne testlike ... –

+0

pourquoi ne pas l'essayer vous-même? –

Répondre

3

Vous verrez le premier. Enfin le bloc est toujours exécuté et en dernier.

2

Est-ce un quiz? Je rigole. Vous verrez le premier s'il y a une exception. L'instruction finally block sera toujours exécutée, de sorte qu'elle sera toujours imprimée.

5

Citant le Java Language Specification, §14.20.4:

Une déclaration d'essai avec un bloc finally est exécuté par la première exécution du bloc d'essai. Ensuite, il y a un choix:

* 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).