Why use IOexception instead of Exception when catching?

后端 未结 3 548
别那么骄傲
别那么骄傲 2020-12-18 03:36

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         


        
3条回答
  •  别那么骄傲
    2020-12-18 04:11

    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.

提交回复
热议问题