@@ -365,8 +365,10 @@ throws a **command-terminating error** which has unexpected behavior when called
365365from 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
372374function 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
405407Because of this behavior, ** use ` Write-Error ` instead** in public commands.
@@ -444,8 +446,9 @@ how exceptions are caught:
444446
445447This example shows a state-changing command using ` $PSCmdlet.ThrowTerminatingError() `
446448in 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
450453For non-state-changing commands (like Get/Test commands), prefer ` Write-Error ` in
451454catch blocks instead.
0 commit comments