IOException in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

The Java IOException is a checked exception that must be handled at compilation time. IOException is the base class for exceptions thrown while accessing data from files, directories, and streams. It represents a class of exceptions that are thrown when an I/O error occurs.

The most common cause due to which an IOException is thrown is attempting to access a file that does not exist at the specified location. Common exception classes that are derived from the IOException base class are EndOfStreamException, FileNotFoundException, DirectoryNotFoundException, etc.

What is IOException in Java?

The IOException is simply an exception thrown when an I/O error occurs. It is also the base class of such exceptions that occur while reading or accessing files, directories, and streams.

The IOException belongs to the class of checked exceptions. Checked exceptions are thrown at compile time. Resolving checked exceptions is necessary to execute a Java program.

There are many subclasses of IOException, such as FileNotFoundException, SSLException, EndOfStreamException, and DirectoryNotFoundException.

When IOException Occurs in Java?

Let's see an example where IOException is thrown at compile-time:

Output:

Explanation:

  • FileNotFoundException is thrown when compiling the above Java program. Hence, the program does not compile and cannot be executed.
  • This exception is thrown to avoid scenarios such as if the named file does not exist, is a directory rather than a regular file, or cannot be opened for reading for some other reason.
  • Apart from the close() and read() methods of the FileReader class, it also throws an IOException, which must be handled.

How to Handle IOException in Java?

We can handle IOException using the try and catch blocks to handle the exception.

Let's understand how to use the try and catch block to handle the IOException in java.

Output:

Explanation:

  • In the above case, We have used try and catch blocks to handle IOException.
  • The above program compiles successfully and does not throw any exception at compile time.
  • However, if the specified file does not exist or any other error occurs, the code within the catch block executes as shown in the output.

IOException Examples

Let's understand the examples of IOExceptions.

UnsupportedEncodingException: This exception occurs when we try to encode the String using an invalid encoding scheme. Let's understand this using an example.

Output:

Explanation:

  • In the above program, we encode a given string using a not-supported format.
  • In such cases, UnsupportedEncodingException is thrown, as shown in the output.

Conclusion

  • IOException is a checked exception which occurs at compile time. It must be resolved before executing a Java program.
  • IOException is the base class of many checked exceptions thrown while reading files, directories, and streams.
  • The try and catch block avoids IOException.
  • FileNotFoundException, UnsupportedEncodingException, and DirectoryNotFoundException are some of the subclasses of the IOException class.