What is finally block ?
finally block is either written after catch block or try block.finally blocks always executes when try block exits.Even if unexpected exception occurs in try block ,finally block is executed.It must be immediately followed by try or catch block.
Example:
/**
* @author Dixit
*
*/
public class FinallyTest {
public static void main(String a[]) {
try {
System.out.println("Try block");
} catch (Exception e) {
System.out.println("catch block.");
} finally {
System.out.println("finally block.");
}
}
}
Output
Try block
finally block.
Usage of finally block
finally block is mostly used to execute important code like closing db connection,closing file resources etc.
Also read Important finally interview questions
Enjoy Learning :)
No comments:
Post a Comment