Catching java exceptions FileNotFound and IOException at the same time

后端 未结 5 1044
你的背包
你的背包 2021-01-02 07:25

Is the FileNotFoundException somehow a \"sub-exception\" of the IOException?

This is my code opening an input stream to a file at the given path:

            


        
5条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-02 07:32

    Yes, as the javadoc shows it, FileNotFoundException is a subclass of IOException.

    If you really wantFileNotFoundException, you must catch only this execption, otherwise catching IOException will also catch any exception subclassing it, like FileNotFoundException any many others.

提交回复
热议问题