python try:except:finally

后端 未结 8 2149
后悔当初
后悔当初 2020-12-23 17:36
# Open new file to write
file = None
try:
    file = open(filePath, \'w\')
except IOError:
    msg = (\"Unable to create file on disk.\")
    file.close()
    return         


        
8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-23 17:42

    It is always advisable to write your logic or code that might throw an exception in the try block and use the finally block for closing the resources.

提交回复
热议问题