Why use IOexception instead of Exception when catching?

后端 未结 6 920
隐瞒了意图╮
隐瞒了意图╮ 2020-12-18 03:31

I can\'t seem to phrase this correctly for the search engine to pick up any meaningful results.

try{
    BufferedReader reader = new BufferedReader( new File         


        
6条回答
  •  长情又很酷
    2020-12-18 04:29

    The difference is there could be other problems inside the code of your try block that could throw other types of Exceptions including subclasses of RuntimeException (which don't have to be declared).

    If you just catch Exception, then you will catch all of those other errors too which may hide a different problem. Also your code inside the catch block can't assume the Exception happened due to an IOException since any kind of exception will be caught.

提交回复
热议问题