You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
| General error handling in public commands |`Write-Error`| Provides consistent, predictable behavior; allows caller to control termination |
477
578
| Pipeline processing with multiple items |`Write-Error`| Allows processing to continue for remaining items |
579
+
| Catching .NET method exceptions | try-catch without setting `$ErrorActionPreference`| .NET exceptions are always caught automatically |
580
+
| Blanket error handling for multiple cmdlets | Set `$ErrorActionPreference = 'Stop'` in try, restore in finally | Avoids adding `-ErrorAction 'Stop'` to each cmdlet |
581
+
| Single cmdlet error handling | Use `-ErrorAction 'Stop'` on the cmdlet | Simpler and more explicit than setting `$ErrorActionPreference`|
478
582
| Assert-style commands |`$PSCmdlet.ThrowTerminatingError()`| Command purpose is to throw on failure |
479
583
| Private functions (internal use only) |`$PSCmdlet.ThrowTerminatingError()` or `Write-Error`| Behavior is understood by internal callers |
480
-
|Any command | Never use`throw`|Poor error messages; unpredictable behavior|
481
-
|Parameter validation attributes |`throw`|Only valid option within `[ValidateScript()]`|
584
+
|Parameter validation in `[ValidateScript()]`|`throw`|Only valid option within validation attributes|
585
+
|Any other scenario in commands | Never use `throw`|Poor error messages; unpredictable behavior|
0 commit comments