in reply to Ending an application gracefully

Have you looked at the END Block?

-Waswas