Skip to content

Error Handling #180

Answered by Jaykul
Ioan-Popovici asked this question in Q&A
Discussion options

You must be logged in to vote

Three things to consider:

If the error is not always terminating

Then you should either Write-Error or $PSCmdlet.WriteError and leave it up to the user to set -ErrorAction Stop when they call your function.

To decide which to use, consider:

If you want the user to debug where the error comes from

Then you can use Write-Error -ErrorAction Stop or throw because the error will come from where it is thrown from.

Otherwise ...

If the error is due to how the command was called, or put another way: if the error is expected and can be fixed outside your command, then you probably want to hide where (in your command) the error came from, so the error appears to come "from" the call to your command.

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by Ioan-Popovici
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants