'Dump data in a file up to the point the python program gets interrupted

I have a program composed of different files where I use try/except blocks and my own logger to catch specific exceptions. At the end of execution a json file with all data is build. How can I make sure that even with errors caught by my try/except blocks or even errors not yet caught by one of such blocks, the program will always try to output that json file up to the point where the exception happened.

I was thinking of using “finally” on each try/except block on each of my scripts to output the json there, but with that many try/except blocks is this the best solution? What is the best practice when it comes to this scenario?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source