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
    Posts
    27
    Rep Power
    0

    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
    import java.io.*;
    
    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 04:23 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    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 04: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
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    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,

    Jos
    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, 08:25 AM
  2. unreported exception java.lang.Exception
    By Marisabel in forum New To Java
    Replies: 6
    Last Post: 05-01-2011, 03:21 AM
  3. Replies: 5
    Last Post: 02-14-2011, 11:39 PM
  4. Replies: 2
    Last Post: 10-08-2010, 12:08 PM
  5. what does finally mean in Java
    By JordashTalon in forum New To Java
    Replies: 2
    Last Post: 01-18-2009, 12: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
  •