Or letting PyGame work for us.
To work with PyGame we must initialize it first, for this job PyGame have pygame module:
import pygame from pygame.locals import *
To initialize every part of PyGame just call pygame.init() to quit your app pygame.quit()
To watch current error use pygame.get_error() but this string is also given to you when pygame.error exception rise. pygame.error is standard PyGame exception raised when any error accrue.
If we need to clean up something before closing PyGame create for it a function and then call pygame.register_quit(written_function) and leave rest for PyGame. You can call it multiple times PyGame will call all of them in reversed order. (First will be the last)