Page 382 - CITS - Computer Software Application -TT
P. 382
COMPUTER SOFTWARE APPLICATION - CITS
ADVERTISE
2 We can also handle these exception using try-catch However, the way which we have used above is not
correct. We have to a give meaningful message for each exception type. By doing that it would be easy to
understand the error. We will use the try-catch block in the following way:
Exception.java
import java.io.*;
class Exception{
public static void main(String args[]) {
FileInputStream file_data = null;
try{
file_data = new FileInputStream(“C:/Users/ajeet/OneDrive/Desktop/programs/Hell.txt”);
}catch(FileNotFoundException fnfe){
System.out.println(“File Not Found!”);
}
int m;
try{
while(( m = file_data.read() ) != -1) {
System.out.print((char)m);
}
file_data.close();
}catch(IOException ioe){
System.out.println(“I/O error occurred: “+ioe);
}
}
}
We will see a proper error message “File Not Found!” on the console because there is no such file in that location.
369
CITS : IT&ITES - Computer Software Application - Lesson 101 - 108 CITS : IT&ITES - Computer Software Application - Lesson 101 - 108