Try catch finally, flux best practice
Bonjour à tous,
Mon problème est le suivant : j'ai deux flux +du code métier générant une exception, qqchose comme ça :
1 - création flux 1
2 - création flux 2
3 - code métier générant une "ApplicationException"
4 - fermeture flux 1
5 - fermeture flux 2
Comment est ce que je peux faire pour fermer ça proprement sans faire d'imbrication de try catch finally ??
Passer par une sous méthode?
un peu de code pour illustrer :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| public class AEAE {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
System.out.println(1);
try {
System.out.println(2);
ex();
System.out.println(3);
} catch (Exception e) {
System.out.println(4);
} finally {
System.out.println(5);
close();
System.out.println(5.1);
close();
System.out.println(5.2);
}
System.out.println(6);
}
public static String ex() throws Exception{
throw new RuntimeException();
}
public static String close() throws Exception{
throw new RuntimeException();
}
} |
res :
Citation:
1
2
4
5
Exception in thread "main" java.lang.RuntimeException
at AEAE.close(AEAE.java:37)
at AEAE.main(AEAE.java:22)
Puis je avoir 5.1 et 5.2 sans rajouter de try?
merci