0% found this document useful (0 votes)
19 views

Program Using Exception Handling

This Java program uses exception handling to read a file and print its contents. It catches FileNotFoundException if the file is not found and IOException if there is an error reading the file. Finally, it closes the file input stream and prints a message when the exception handling is completed.

Uploaded by

GokulKannan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Program Using Exception Handling

This Java program uses exception handling to read a file and print its contents. It catches FileNotFoundException if the file is not found and IOException if there is an error reading the file. Finally, it closes the file input stream and prints a message when the exception handling is completed.

Uploaded by

GokulKannan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

/*PROGRAM USING EXCEPTION HANDLING*/

import java.io.*;
class Test
{
public static void main(String args[])
{
FileInputStream fis=null;
try
{
fis = new FileInputStream (new File (args[0]));
int ch;
while ((ch = fis.read()) != -1)
{
System.out.print ((char) ch);
}
}
catch (FileNotFoundException e)
{
System.out.println("File not found!");
}
catch (IOException e)
{
System.out.println("Unable to read file!");
}
finally
{
System.out.println();
System.out.println("Exception completed.");
try
{
if(fis!=null){
fis.close();
}
}
catch (IOException ioe)
{
System.out.println("In finally.");
}
}
}
}

C:\jdk1.3\bin>javac Test.java
C:\jdk1.3\bin>java Test abc.txt
1.Which is the 5th planet in the solar system?
Ans:Jupiter
2.When did second world war ended?
Ans:1945
3.Who is the eldest son of kunthi?
Ans:Karnan
4.In which Direction does a Muslim pray?
Ans:Mecca?s Direction
5.When did soviet union breakdown?
Ans:1989
Exception completed.
C:\jdk1.3\bin>java Test AbCd.txt
File not found!
Exception completed.

You might also like