Cleanup code and catch exceptions