We can write the finally block immediately after the try block by skipping the catch block.
Syntax -
try {
//Code inside the try block
}
finally {
//Code inside the finally block
}
Lets implement this on Eclipse IDE -
1. Launch Eclipse IDE, create a new Java Class 'FinallyDemo3.java' with main( ) method in the existing Java Project 'Project 46' as shown below -
2. Create a try block and write a statement which prints "I am in try block" text as shown below -
3. Create a finally block without creating catch block and write a statement which prints "I am in finally block" text as shown below -
4. Save and Run the Java class 'FinallyDemo3.java' and observe that the statements in the try block and finally block got executed and the output is displayed as shown below -
Hence finally block can be written immediately after the try block.
Please comment below to feedback or ask questions.
No comments:
Post a Comment