Skip to content

Commit 70d34ca

Browse files
Copilotjohlju
andcommitted
Fix inconsistencies in error handling documentation
Co-authored-by: johlju <[email protected]>
1 parent 95ddc8d commit 70d34ca

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

CONTRIBUTING.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,10 @@ throws a **command-terminating error** which has unexpected behavior when called
365365
from another command.
366366

367367
**The Problem**: When a command uses `$PSCmdlet.ThrowTerminatingError()`, the
368-
error behaves like a non-terminating error to the caller unless the caller
369-
explicitly uses `-ErrorAction 'Stop'`. This creates confusing behavior:
368+
error is command-terminating (stops that command) but NOT script-terminating. The
369+
calling function continues executing unless the caller sets
370+
`$ErrorActionPreference = 'Stop'` or wraps the call in try-catch. This creates
371+
confusing behavior:
370372

371373
```powershell
372374
function Get-Something
@@ -398,8 +400,8 @@ function Start-Operation
398400
Write-Output 'Operation completed'
399401
}
400402
401-
# Caller must use -ErrorAction 'Stop' to prevent continued execution
402-
Start-Operation -ErrorAction 'Stop'
403+
# Caller must set ErrorActionPreference = 'Stop' or use try-catch to prevent continued execution
404+
Start-Operation -ErrorAction 'Stop' # This alone is NOT sufficient!
403405
```
404406

405407
Because of this behavior, **use `Write-Error` instead** in public commands.
@@ -444,8 +446,9 @@ how exceptions are caught:
444446

445447
This example shows a state-changing command using `$PSCmdlet.ThrowTerminatingError()`
446448
in a catch block. This is acceptable for commands with `SupportsShouldProcess` where
447-
operation failures must terminate. Note that callers must use `-ErrorAction 'Stop'`
448-
to catch this error.
449+
operation failures must terminate. **Important:** Callers must set
450+
`$ErrorActionPreference = 'Stop'` or wrap the call in try-catch to stop execution
451+
on error (using `-ErrorAction 'Stop'` alone is insufficient).
449452

450453
For non-state-changing commands (like Get/Test commands), prefer `Write-Error` in
451454
catch blocks instead.

0 commit comments

Comments
 (0)