The try/catch block is a staple of error handling.  In PowerShell, one common anti-pattern I see in try/catch blocks is the use of $error[0].

If you are not familiar with $error, it's an automatic variable that contains all the error records from the session.  It's a stack, so the last error that occurred is in $error[0].

I've recently seen some public scripts that use $error[0] in the catch block

So what happens if DoSomeLoggingOfContext throws or writes an error?  $error will reflect the last error (terminating or non-terminating), so my LogError will be logging the wrong error and the terminating error I caught is lost.

A better option is to use $_ to represent the caught exception.

This ensures that you are referencing the caught error and not just whatever was last on the stack.  

Happy Error Handling!

Want more great reading? Check out my reading list!