小さなプロジェクト用にマップ エディターを作成しましたがPython2.7.9
、未処理の例外が発生した場合に編集したデータを保持する方法を探しています。私のエディターには既にデータを保存する方法があり、現在の解決策は、次のtry..finally
例のように、メイン ループをブロックにラップすることです。
import os, datetime #..and others.
if __name__ == '__main__':
DataMgr = DataManager() # initializes the editor.
save_note = None
try:
MainLoop() # unsurprisingly, this calls the main loop.
except Exception as e: # I am of the impression this will catch every type of exception.
save_note = "Exception dump: %s : %s." % (type(e).__name__, e) # A memo appended to the comments in the save file.
finally:
exception_fp = DataMgr.cwd + "dump_%s.kmap" % str(datetime.datetime.now())
DataMgr.saveFile(exception_fp, memo = save_note) # saves out to a dump file using a familiar method with a note outlining what happened.
これは、何が起こっても、エディターsaveFile()
がクラッシュした場合にエディターの現在の状態を (そうするように装備されている範囲で) 保持しようとする最善の方法のようです。try
しかし、メインループ全体をブロックにカプセル化することは、実際に安全で効率的で、良い形なのだろうか。それは...ですか?リスクや問題はありますか?より良い、またはより従来の方法はありますか?