That may not be the best title for this request.
I'm aware that I can wrap Powershell code in a try...catch
block. I am also aware that I can set the $ErrorActionPreference
variable.
However, rather than wrap the entire script in a big Try...Catch
block, I would like to have some default code that will run when an unhandled exception occurs.
For instance, let's say some unexpected error occurs that is not caught by a try...catch
. I would like PowerShell, before exiting (assuming ErrorActionPreference is STOP) to run a particular block of code so that the unexpected error can be logged or reported on.
Is there a way to do that?