Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Example to show Finally exception in java 
 

Finally block are the block, that executed when the try block exits.

 

Example to show Finally exception in java

                         

Finally block are the block, that executed when the try block exits. This block executed even after the unexpected exception occured.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.

Understand with Example.

In this Tutorial we want to describe you a code that helps you to handle finally exceptions. The Finally block succeed when the try block exists and will  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 "girish.txt" does not occurs it will throw File not Found exception. and also executes the finally block of the program.

1)File Input Stream  - This is used to decode the byte and further convert it into a character. stream

Inside the main method the input stream include a  fileinputstream object that store and convert  convert the byte code to a character stream from a file name "girish.txt".In case there is no such file on the name of "girish.txt".The catch block caught the unexpected exception .Later on finally block print the exception.

FinallyException.java


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 = "girish.txt";
             try {
            fis1 = inputStream(fileName);
        catch (FileNotFoundException ex) {
            System.out.println("FileNotFoundException occured");
        catch (Exception ex) {
            System.out.println("genreal exception occured");
        }
        System.out.printlnFinallyException.class.getName() " ended");
    }

 

Output of the program

FileNotFoundException occured
FinallyException ended


Download source code

                         

» View all related tutorials
Related Tags: c exception io stack type exec uri lock this block exe row handle program to execution ram each pos ast

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.