Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By JosAH

Thread: How to use finally exception in Java?

  1. #1
    Java Example is offline Moderator
    Join Date
    Sep 2014
    Rep Power

    Default How to use finally exception in Java?

    In this tutorial, we will discuss about Finally exception with example. Finally blocks are executed when the try block exits even after the unexpected exception had occurred. The Run time always execute the expression in finally block irrespective of the try block. This block is known for recovering and preventing from resources leak. On closing of the file and recovering of a file, you need to place the expression in the finally block.

    In the example below, Finally block succeed when the try block exists and will be executed when an unexpected exception occurs in the code. The program given below describes how to use the finally block. In this program if the file "finally.txt" does not exists it will throw File not Found exception. and also executes the finally block of the program.

    Java Code: Example of Finally Exception
    public class FinallyException {
        public static FileInputStream inputStream(String fileName)
                throws FileNotFoundException {
            FileInputStream fis = new FileInputStream(fileName);
            System.out.println("f1: File input stream created");
            return fis;
        public static void main(String args[]) {
            FileInputStream fis1 = null;
            String fileName = "finally.txt";
            try {
                fis1 = inputStream(fileName);
            } catch (FileNotFoundException ex) {
                System.out.println("FileNotFoundException occured");
            } catch (Exception ex) {
                System.out.println("genreal exception occured");
    Output of the program:

    FileNotFoundException occured
    FinallyException ended
    Last edited by Java Example; 09-26-2014 at 05:23 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power

    Default Re: How to use finally exception in Java?

    Where does the code print out this message? I don't see it in the code. Where did it come from?
    FinallyException ended

    Finally blocks are executed when
    Where is the finally block that statement talks about?

    It looks like the last lines of the program are missing.
    Last edited by Norm; 09-26-2014 at 05:30 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    Default Re: How to use finally exception in Java?

    It is just another sloppy, half-understood example; I already notified the forum owner a couple of days ago and asked him if he really wants this nonsense in his forums.

    kind regards,

    Norm likes this.
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Finally Block in Exception
    By AbbasElesgerov in forum New To Java
    Replies: 2
    Last Post: 06-01-2013, 09:25 AM
  2. unreported exception java.lang.Exception
    By Marisabel in forum New To Java
    Replies: 6
    Last Post: 05-01-2011, 04:21 AM
  3. Replies: 5
    Last Post: 02-15-2011, 12:39 AM
  4. Replies: 2
    Last Post: 10-08-2010, 01:08 PM
  5. what does finally mean in Java
    By JordashTalon in forum New To Java
    Replies: 2
    Last Post: 01-18-2009, 01:28 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts