IOException Class
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Signals that an I/O exception of some sort has occurred.
Remarks
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.
Constructors
Constructs an IOException with null as its error detail message.
A constructor used when creating managed representations of JNI objects; called by the runtime.
Constructs an IOException with the specified detail message.
Constructs an IOException with the specified detail message and cause.
Constructs an IOException with the specified cause and a detail message of (cause==null ? null : cause.toString()) (which typically contains the class and detail message of cause ).
Fields
| is_generated | (Inherited from Throwable) |
Properties
Returns the cause of this throwable or null if the cause is nonexistent or unknown.
The handle to the underlying Android instance.
Creates a localized description of this throwable.
Returns the detail message string of this throwable.
This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.
This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.
Methods
Appends the specified exception to the exceptions that were suppressed in order to deliver this exception.
Fills in the execution stack trace.
Provides programmatic access to the stack trace information printed by #printStackTrace() .
Returns an array containing all of the exceptions that were suppressed, typically by the try -with-resources statement, in order to deliver this exception.
Initializes the cause of this throwable to the specified value.
Prints this throwable and its backtrace to the standard error stream.
Prints this throwable and its backtrace to the specified print stream.
Prints this throwable and its backtrace to the specified print writer.
Sets the Handle property.
Sets the stack trace elements that will be returned by #getStackTrace() and printed by #printStackTrace() and related methods.
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.
Java io ioexception что это
Note that the detail message associated with cause is not automatically incorporated into this exception’s detail message.
IOException
- Summary:
- Nested |
- Field | |
- Detail:
- Field | |
- Method
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2022, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.