Running code on program termination #1783
-
Is there a way to run a function when the program is terminated |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I would wrap my code in a call and watch for the terminated error message. Edit: This snippet allows for tidy-up when main function errors. You can inspect the err message to see if it matches the terminated error message and thus do some special action on terminate. |
Beta Was this translation helpful? Give feedback.
-
An alternative way would be to use os.pullEventRaw and thus not have the automatic termination error throwing. Your code will have to handle the terminate request event itself. |
Beta Was this translation helpful? Give feedback.
I would wrap my code in a call and watch for the terminated error message.
I think I have a code snippet for this somewhere, I'll have a look for it.
Edit: This snippet allows for tidy-up when main function errors. You can inspect the err message to see if it matches the terminated error message and thus do some special action on terminate.