Why use IOexception instead of Exception when catching?

后端 未结 6 917
隐瞒了意图╮
隐瞒了意图╮ 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:26

    The reason is whenever you program, you have to think about all the possibilities and it is useful to do something for a specific error. Exception is a catch all method of catching errors and will handle all exceptions the same. IOException will catch any IO Exceptions so it will treat file not found and and other IO Exception (like EOFException) the same. FileNotFoundException will only catch file not found exceptions so you can handle it instead of just logging it.

    Some errors will happen and being able to handle each individual case keeps your program running. In this case, file not found can make you select another file so the program doesn't crash and it handles the situation.

提交回复
热议问题