+ 6

What will happen if you put return statement or System.exit () on try or catch block? Will finally block execute?

2nd Jan 2017, 5:40 PM
sadiya zainab
sadiya zainab - avatar
2 ответов
+ 6
Answer of this question in Java is that finally block will execute even if you put a return statement in the try block or catch block but finally block won't run if you call System.exit() from try or catch block.
3rd Jan 2017, 4:53 AM
sadiya zainab
sadiya zainab - avatar
0
Oops, I was dead wrong with my previous answer. sadiya is correct. Here's what I tested: public static void main([...]){ System.out.println(testReturn()); } public static int testReturn(){ int a = 1; int b = 2; try { // System.exit(0); return a; } catch(Exception e){ return b; } finally{ System.out.println("Wait, here's a sout!"); } } Outputs : Wait, here's a sout! 1 if you uncomment exit(0), you get no output at all. TIL!
3rd Jan 2017, 5:40 PM
Nsyse
Nsyse - avatar