Added database closure in a try/except statement