Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

PShell helper commands do not throw exceptions (errors)

They already do. The PowerShell functions are thin wrappers; the throwing happens inside the .NET static methods they call.

So any non-zero host result or any error text from the TCC side raises InvalidOperationException, which surfaces in PowerShell as a terminating error.

Things that legitimately would not throw today (and arguably are correct):
  • TCC 'echo hi' when echo hi writes to stderr but returns 0 — nResult == 0 and no errortext, so no throw. That matches TCC's normal "stderr is just output" behavior.
  • A TCC command that returns a non-zero exit code via EXIT n — that does throw, because nResult carries the command's return value. If you'd rather have $LASTEXITCODE-style semantics there, that's a deliberate design decision worth revisiting, but it isn't a bug.
So the only real gap I see is the third helper: Get-TccVariable already throws on a null/empty name, but it does not throw when the variable is undefined — ExpandValue("%nosuchvar") will typically return the literal %nosuchvar (TCC's default behavior) with nResult == 0. If you want "undefined variable ⇒ throw" semantics, that would need to be added explicitly in Get-TccVariable (compare the result to the input, or check via %@if[isdef…] first), since the host callback won't flag it.
 
Thanks Rex.

Code:
try {
    $result = [TakeCommand.PowerShellHost]::InvokeCommand("which ech")

    "Output: $result"
}
catch {
    "Caught TCC error: $($_.Exception.Message)"
}

Write-Host --------------------------------------------

try {
    $result = [TakeCommand.PowerShellHost]::InvokeCommand("which echo")

    "Output: $result"
}
catch {
    "Caught TCC error: $($_.Exception.Message)"
}

Proof:
Code:
R:\>pshell test.ps1
Caught TCC error: Exception calling "InvokeCommand" with "1" argument(s): "ech is an unknown command"
--------------------------------------------
Output: echo is an internal command

R:\>

Joe
 
Back
Top