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
- In catch blocks, pass original exception using `-Exception`
99
99
- Always use `return` after `Write-Error` to avoid further processing
100
100
-**Avoid `$PSCmdlet.ThrowTerminatingError()` in public commands** - creates command-terminating (not script-terminating) errors; callers must set `$ErrorActionPreference = 'Stop'` OR use try-catch (using `-ErrorAction 'Stop'` alone is insufficient)
101
-
- Acceptable only in: assert-style commands, private functions, or state-changing catch blocks where operation failures must prevent further state changes
101
+
- Acceptable only in: private functions, or state-changing catch blocks where operation failures must prevent further state changes
102
+
- Assert-style commands should use `Write-Error` for standard behavior
102
103
- Use `throw` only in `[ValidateScript()]` parameter validation attributes - it's the only valid mechanism there
103
104
- .NET method exceptions (e.g., SMO methods) are always caught in try-catch blocks without needing to set `$ErrorActionPreference`
| 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 |
617
618
| Single cmdlet error handling | Use `-ErrorAction 'Stop'` on the cmdlet | Simpler and more explicit than setting `$ErrorActionPreference`|
618
619
| Calling commands that use ThrowTerminatingError | Set `$ErrorActionPreference = 'Stop'` in caller OR wrap in try-catch | Using `-ErrorAction 'Stop'` alone is NOT sufficient; caller continues after error |
619
-
| Assert-style commands |`$PSCmdlet.ThrowTerminatingError()`|Command purpose is to throw on failure|
620
+
| Assert-style commands |`Write-Error`|Provides standard behavior; `-ErrorAction 'Stop'` alone works correctly for callers|
620
621
| State-changing commands (catch blocks) |`$PSCmdlet.ThrowTerminatingError()`| Prevents partial state changes; caller must set `$ErrorActionPreference = 'Stop'` OR wrap in try-catch |
621
622
| Private functions (internal use only) |`$PSCmdlet.ThrowTerminatingError()` or `Write-Error`| Behavior is understood by internal callers |
622
623
| Parameter validation in `[ValidateScript()]`|`throw`| Only valid option within validation attributes |
0 commit comments