What can I do to make my code throw an IOException
?
I have tried reading from and writing to a file that does not exist. But that throws a FileNotFoundException
.
What are some specific things that throw an IOException
?
What can I do to make my code throw an IOException
?
I have tried reading from and writing to a file that does not exist. But that throws a FileNotFoundException
.
What are some specific things that throw an IOException
?
If you want to throw an IOException()
deliberately, you should simply throw a new IOException()
like so:
throw new IOException();
This is the correct way to deliberately throw an exception. Deliberately causing an exception to be thrown is not good practice, as it will make the exception look genuine.
Here is the full list of exception subclasses that can throw an IOException()
:
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
This list is from https://docs.oracle.com/javase/8/docs/api/java/io/IOException.html.