Do files get closed during an exception exit?

前端 未结 4 1239
南笙
南笙 2020-12-03 03:08

Do open files (and other resources) get automatically closed when the script exits due to an exception?

I\'m wondering if I need to be closing my resources during my

4条回答
  •  隐瞒了意图╮
    2020-12-03 03:21

    I, as well as other persons in this thread, are left with the question, "Well what is finally true?"

    Now, supposing that files are left open in a premature program termination -- and there are a lot of such cases besides exceptions due to file handling -- the only safe way to avoid this, is to read the whole (or part of the) file into a buffer and close it. Then handle the contents in the buffer as needed. This is esp. the case for global search, changes, etc. that have to be done on the file. After changes are done, one can then write the whole buffer to the same or other file at once, avoiding the risk to leave the the newly created file open -- by doing a lot readings and writings -- which is the worst case of all!

提交回复
热议问题