Google+

Like Us On Facebook


Total Pageviews

237. Using finally block immediately after the try block








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.

Nested try catch blocks will be explained in the next post.






No comments:

Blog Index