IOExceptions的具体案例

时间:2017-04-05 19:35:30

标签: java exception-handling

如何让我的代码抛出FileNotFoundException

我尝试过读取和写入不存在的文件。但这会引发IOException

有哪些特定内容会引发{{1}}?

1 个答案:

答案 0 :(得分:1)

如果你想故意抛出IOException(),你应该简单地抛出一个新的IOException()

throw new IOException();

这是故意抛出异常的正确方法。故意造成异常抛出并不是一种好的做法,因为它会使异常看起来真实。

以下是可以抛出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

此列表来自https://docs.oracle.com/javase/8/docs/api/java/io/IOException.html

相关问题