How to Use Finally Block in java


The example is to show how to use Finally block in Java.

Source Code

package com.beginner.examples;

import java.net.MalformedURLException;
import java.net.URL;
 
public class FinallyBlockExample {
    public static void main(String[] args){
		try 
		{
			URL url = new URL("https://www.yahoo.com/");
	        System.out.println(url);
		} 
		catch (MalformedURLException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			System.out.println("OK");
		}
    }
}

Output:

https://www.yahoo.com/
OK

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments