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

Overview

The Java IOException is a checked exception that must be handled at compilation time. IOException is the base class for such exceptions which are 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 which are derived from IOException base class are EndOfStreamException, FileNotFoundException, DirectoryNotFoundException etc.

What is IOException in Java?

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

The IOException belongs to the class of checked exceptions. Checked exceptions are those exceptions which are thrown at compile-time. It is necessary to resolve checked exceptions to execute a Java program.

There are many subclasses of IOException like FileNotFoundException, SSLException, EndOfStreamException, DirectoryNotFoundException, etc.

When IOException Occurs in Java?

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

Output:

Explanation:

  • FileNotFoundException is thrown on compiling the above Java program. Hence, the program does not compile and hence 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 for some other reason cannot be opened for reading.
  • Apart from close() and read() methods of FileReader class also throw IOException which must be handled.

How to Handle IOException in Java?

We can handle IOException by 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:

  • We have used try and catch blocks to handle IOException in the above case.
  • 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 catch block executes as shown in the output.

IOException Examples

Let's understand the types of IOExceptions examples.

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 are encoding a given string using a not-supported encoding 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 to execute a Java program.
  • IOException is the base class of a lot of checked exceptions which are thrown while reading files, directories, and streams.
  • The try and catch block is used to avoid IOException.
  • FileNotFoundException, UnsupportedEncodingException, DirectoryNotFoundException are some of the subclasses of IOException class.