Is it possible to handle an error from a function deferred until exit?

In a console application, I want to call a function that will perform some actions upon exit of the program. I use a defer block to do that. I also want to handle any errors that may arise from this clean-up function, but have discovered that errors cannot be thrown out of a defer body.

Is there a way for me to (a) handle any errors that may arise in this deferred clean-up function, or (b) call the clean-up function on exit in some way other than using defer? In C (where I'm coming from), I would just register the clean-up function with atexit, which would call that function normally allowing the error-handling routines also run normally.

If the answer is just something I overlooked in the documentation, please feel free to point me where I should be looking. Also, I reviewed the following forum thread:

It did not seem to address my question. But it was a helpful read all the same.


I don't recall cleanup mechanism other than defer. I think the closest would be to use a do-catch block inside that very defer.

1 Like

Ahh, perfect! I'll try it that way. Thank you so much.

Terms of Service

Privacy Policy

Cookie Policy