Share on Google+Share on Google+

Java file exception

This section illustrates you the concept of file exception.

Java file exception

This section illustrates you the concept of file exception.

Exceptions are the errors that occurs at runtime. It is either generated by the Java Virtual Machine (JVM) in response to an unexpected condition or it is generated by a code.

 In the given example, we have specified a file in order to read it but the system couldn't find the file and throws an exception.

Here is the code:


public class FileException {
	public static void main(String[] args) {
		try {
			File f = new File("C:/newFile.txt");
			BufferedReader reader = new BufferedReader(new FileReader(f));
			String str = "";
			while ((str = reader.readLine()) != null) {
		} catch (Exception e) {

Through the above code, we will be able to understand the concept of file exception.

Output: C:\newFile.txt (The system cannot find the file specified)


Posted on: April 14, 2006 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.