Finally block in Java

Posted in /  

Finally block in Java

    Java allows the use of the finally block that allows you to execute the critical code. This block will always get executed whether the exception is handled or not. the finally block will always come after the try-and-catch block. Generally, the finally block is used for the code like cleanup like closing file, connection etc.

    Example

    import java.util.regex.*;  
    public class Simple{  
    public static void main(String args[]){  
    try{  
       int val=25/4;  
       System.out.println(val);  
      }  
      catch(NullPointerException e){System.out.println(e);}  
      finally{System.out.println("finally block");}  
      System.out.println("rest code");  
      }  
    }

    Output

    Finally block in Java examples

    People are also reading:

    Ramya
    Written by

    Ramya Shankar

    A cheerful, full of life and vibrant person, I hold a lot of dreams that I want to fulfill on my own. My passion for writing started with small diary entries and travel blogs, after which I have moved on to writing well-researched technical content. I find it fascinating to blend thoughts and research and shape them into something beautiful through my writing.