Program Using Exception Handling
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.