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 |
583
601
| Pipeline processing with multiple items |`Write-Error`| Allows processing to continue for remaining items |
584
602
| Catching .NET method exceptions | try-catch without setting `$ErrorActionPreference`| .NET exceptions are always caught automatically |
585
-
| Blanket error handling for multiple cmdlets | Set `$ErrorActionPreference = 'Stop'` in try, restore in finally | Avoids adding `-ErrorAction 'Stop'` to each cmdlet |
603
+
| Blanket error handling for multiple cmdlets | Set `$ErrorActionPreference = 'Stop'` in try, restore in finally | Avoids adding `-ErrorAction 'Stop'` to each cmdlet; also catches ThrowTerminatingError from child commands|
586
604
| Single cmdlet error handling | Use `-ErrorAction 'Stop'` on the cmdlet | Simpler and more explicit than setting `$ErrorActionPreference`|
605
+
| Calling commands that use ThrowTerminatingError | Set `$ErrorActionPreference = 'Stop'` or use `-ErrorAction 'Stop'`| Required to catch these command-terminating errors; without it, caller continues after error |
587
606
| Assert-style commands |`$PSCmdlet.ThrowTerminatingError()`| Command purpose is to throw on failure |
607
+
| State-changing commands (catch blocks) |`$PSCmdlet.ThrowTerminatingError()`| Prevents partial state changes; caller must use `-ErrorAction 'Stop'` or set `$ErrorActionPreference`|
588
608
| Private functions (internal use only) |`$PSCmdlet.ThrowTerminatingError()` or `Write-Error`| Behavior is understood by internal callers |
589
609
| Parameter validation in `[ValidateScript()]`|`throw`| Only valid option within validation attributes |
590
610
| Any other scenario in commands | Never use `throw`| Poor error messages; unpredictable behavior |
0 commit comments