finally in exception handling

后端 未结 8 1720
甜味超标
甜味超标 2021-01-18 17:08

What exactly does a finally block in exception handling perform?

8条回答
  •  长发绾君心
    2021-01-18 17:49

    The finally block always executes, regardless of whether or not the exception was thrown. The classic use example I can think of is closing files.

    FileOutputStream stream = null;
    try{
        // do stuff with the stream here
    } catch (IOException ex){
        // handle exception
    } finally{
        // always close the stream
        if(stream != null){
            stream.close();
        }
    }
    

提交回复
热议问题