File Handling Exception Example
This program demonstrates exception handling in file operations.
Problem Statement
Write a Java program that handles FileNotFoundException during file reading.
Source Code
| 1 | import java.io.*; |
| 2 | public class FileHandlingExceptionExample { |
| 3 | public static void main(String[] args) { |
| 4 | System.out.println("Eduinq File Handling Exception Example"); |
| 5 | try { |
| 6 | FileReader fr=new FileReader("nonexistent.txt"); |
| 7 | BufferedReader br=new BufferedReader(fr); |
| 8 | System.out.println("Eduinq File Content: "+br.readLine()); |
| 9 | } catch(FileNotFoundException e) { |
| 10 | System.out.println("Eduinq Exception: File Not Found"); |
| 11 | } catch(IOException e) { |
| 12 | System.out.println("Eduinq Exception: IO Error"); |
| 13 | } |
| 14 | } |
| 15 | } |
Program Output
Eduinq File Handling Exception Example Eduinq Exception: File Not Found
Explanation
File operations often throw checked exceptions. - FileNotFoundException occurs when file is missing. - Catch block handles the error gracefully. - Additional IOException catch ensures broader error coverage. - In enterprise systems, file handling exceptions ensure reliable data access. Conclusion: Shows exception handling in file operations.