Class IOException
Note that the detail message associated with cause is not automatically incorporated into this exception’s detail message.
IOException
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2022, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.
Java.IO.Ioexception
This tutorial demonstrates the java.io.ioexception in Java.
the java.io.IOException in Java
- Reading a file, but that file doesn’t exist.
- Reading a file but don’t have permission.
- Writing a file, but the disc space is not available.
- When reading a stream, but that stream is closed by some other process.
- When reading a network file, but that network is disconnected.
The Java.IO.IOexception provides exceptions for system input and output data streams and serialization.
There are also many other reasons for the IO exceptions in Java. The table below shows the types or subclasses of exceptions in Java:
The above exceptions are the types of subclasses of the main IO exception. Let’s try an example that throws the IOException in Java:
The code above will throw the IO FileNotFoundException because the file we are accessing doesn’t exist. See output:
To handle these exceptions, we use try-catch blocks to stop JVM from crashing the code. See the solution:
Now the code above will print the same error information but without crashing the code. See output:
Let’s try another example that throws the End of File exception ( EOFException ) in Java:
The code throws EOFException because it’s trying to read a file, and the end of the file has arrived. See output:
Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.
What throws an IOException in Java?
java.io.IOException seems to be the most common type of exception, and coincidentally, it seems to also be the most ambiguous.
I keep seeing the throws IOException whenever writing with sockets, files, etc. I’ve never actually had one fired on me, however, so I’m wondering what it is that is supposed to fire the exception. The documentation isn’t very helpful in explaining what’s going on:
Signals that an I/O exception of some sort has occurred. This class is the general class of exceptions produced by failed or interrupted I/O operations.
What are some instances where an IOException would be thrown, and how is it supposed to be used?
![]()
3 Answers 3
Assume you were:
- Reading a network file and got disconnected.
- Reading a local file that was no longer available.
- Using some stream to read data and some other process closed the stream.
- Trying to read/write a file, but don’t have permission.
- Trying to write to a file, but disk space was no longer available.
There are many more examples, but these are the most common, in my experience.
![]()
In general, I/O means Input or Output. Those methods throw the IOException whenever an input or output operation is failed or interpreted. Note that this won’t be thrown for reading or writing to memory as Java will be handling it automatically.
Here are some cases which result in IOException .
- Reading from a closed inputstream
- Try to access a file on the Internet without a network connection
![]()
Java documentation is helpful to know the root cause of a particular IOException.
Just have a look at the direct known sub-interfaces of IOException from the documentation page:
ChangedCharSetException, CharacterCodingException, CharConversionException, ClosedChannelException, EOFException, FileLockInterruptionException, FileNotFoundException, FilerException, FileSystemException, HttpRetryException, IIOException, InterruptedByTimeoutException, InterruptedIOException, InvalidPropertiesFormatException, JMXProviderException, JMXServerErrorException, MalformedURLException, ObjectStreamException, ProtocolException, RemoteException, SaslException, SocketException, SSLException, SyncFailedException, UnknownHostException, UnknownServiceException, UnsupportedDataTypeException, UnsupportedEncodingException, UserPrincipalNotFoundException, UTFDataFormatException, ZipException
Most of these exceptions are self-explanatory.
A few IOExceptions with root causes:
EOFException: Signals that an end of file or end of stream has been reached unexpectedly during input. This exception is mainly used by data input streams to signal the end of the stream.
SocketException: Thrown to indicate that there is an error creating or accessing a Socket.
RemoteException: A RemoteException is the common superclass for a number of communication-related exceptions that may occur during the execution of a remote method call. Each method of a remote interface, an interface that extends java.rmi.Remote, must list RemoteException in its throws clause.
UnknownHostException: Thrown to indicate that the IP address of a host could not be determined (you may not be connected to Internet).
MalformedURLException: Thrown to indicate that a malformed URL has occurred. Either no legal protocol could be found in a specification string or the string could not be parsed.
What is a Java IOException?

IOException means any input-output exception that can occur while reading a file or writing into a file, basically accessing the file system. There can be many ways in which this exception can occur. The most common I/O exceptions are FileNotFoundException, InterruptedIOException, SocketException, UnsupportedDataTypeException.
What is Java I/O Exception?
Java IOException extends the Java Exception class. To get the Serializable form, the interface Serializable is implemented by the class that extends IOException. This is useful when you have custom exception classes for your project. For the program to exit gracefully in an exception case, we should surround it using try/catch block. This also means that IOException is a checked exception.
Most IDEs will remind you to put the try/catch as soon as you use classes like FileInputStream, BufferedReader, DataInputStream, etc. Note that in the above program, though we have used IOException, we can also use a more specific FileNotFoundException (which is a subclass of IOException). However, IOException can handle other I/O errors as well. If we want, we can add both:
This will not give an error, but the compiler will give you a warning, indicating that a more specific exception has already been handled. Note that the order is important here. If you first catch IOException, you will get an error while handling FileNotFoundException as it is redundant and can be handled by IOException itself. Note that we can also throw this exception as an ApplicationException, RuntimeException, or BusinessException to be handled by some other class:
In this case, we have to add the throws clause to the method as well:
Conclusion
Java IOException is used to handle file-related errors like searching a file, reading it, or writing into it. It is a checked or compile-time exception that can be handled in the code by the developers.
There are many subclasses of IOException which can be used in the program instead of using the general IOException as well. IOException is a part of the java.io package and extends the java.lang.Exception class. IOExceptions can be handled using a try/catch block or by catching and then throwing the exception for some other class to handle.
People are also reading:
- Java Keywords
- Data Types in Java
- Java If-else
- How to declare, initialize and populate Java int arrays?
- Java Operators
- Queue in Java
- Enum Example in Java
- History of Java
- Java Program Internal
- Hello Java Program
What are Java IOExceptions?
When you read a particular file or access the file using the wrong path, you get the Java IOException. It is a checked exception that takes place during the compilation of Java programs.
How to handle Java IOExceptions?
There are two ways to handle Java IOExceptions: 1. Using good programming while writing programs 2. Using the try-catch block to avoid any kind of exceptions in a program.
What is the major difference between IOExceptions and exceptions?
While exceptions take place either at a compile time or runtime, IOExceptions take place only at the compile time.
How is an error different from an exception in Java?
Both an error and an exception are the subclasses of the throwable class. While an error is a problem that takes place due to the shortage of system resources, an exception takes place at the runtime or compile time.