You know finally block is useful for preventing resource leaks.
Look at the below code, may be interesting for you if you didn't tried yet.
output:
Hello
in finally
str : Hello dude
Now try to give System.exit(0); before return hi; statement at line 9.
now run the sample code and check the output.
Look at the below code, may be interesting for you if you didn't tried yet.
package com.j4b.samples; public class FinallyReturn { @SuppressWarnings("finally") public static String display(String hi){ try{ System.out.println("Hello"); return hi; } finally{ System.out.println("in finally"); return "Hello dude"; } } public static void main(String[] args) { String str = display("Hi Praveen"); System.out.println("str : "+str); } }
output:
Hello
in finally
str : Hello dude
Now try to give System.exit(0); before return hi; statement at line 9.
now run the sample code and check the output.
Thanks for adding new Concept in ma Knowlwdge
ReplyDelete