Do not throw uncaught exceptions in a destructor

Fix bugs reported by SonarCloud