Powershell: Set-PSBreakpoint -TerminatingError

Created on 24 Jan 2018  路  2Comments  路  Source: PowerShell/PowerShell

Set-PSBreakPoint should have a new parameterset to allow you to break when a terminatingerror is encountered.

Set-PSBreakPoint -TerminatingError -Action {
Write-Host "Here is where the error occured: $(Get-PSCALLSTACK)"
continue
}

Set-PSBreakPoint -TerminatingError -Action {
Write-Host "Here is where the error occured: $(Get-PSCALLSTACK). Entering the Debugger"
break
}

Issue-Enhancement WG-Interactive-Debugging

All 2 comments

I assume the above would break on an "uncaught" terminating error. This would be very useful and something we would want to surface via the debug UI in VSCode.

Another option might be to add a -FirstThrown switch. Sometimes in our zeal to squash errors, a terminating error would never be considered "uncaught" because we'd have a catch {} buried in our code somewhere that swallows the terminating error. In this case, it is quite handy to be able to trigger a breakpoint where the terminating error is initially thrown. It would also be handy to have a filter parameter to eliminate false triggers e.g. -FirstThrownFilter {$exception -is [Management.Automation.ItemNotFoundException] or -FirstThrownFilter {(Get-PSCallStack)[0].Command -eq 'SuspectedFunc'}

Nice point. We should be able to break on both ANY terminating error OR any UNCAUGHT terminating error.

Was this page helpful?
0 / 5 - 0 ratings