-
Notifications
You must be signed in to change notification settings - Fork 7.6k
How, exactly, does PowerShell decide whether .ThrowTerminatingError() terminates only the statement? #6098
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I believe we terminating pipeline #2860 (comment) |
If it were that simple, then the output of try
{
1 | a
Write-Host 'statement after'
}
catch
{
Write-Host 'catch'
} would be |
You should view this from the perspective of the If a terminating error occurs - whether script- or statement-terminating - the entire (A nonterminating error, by contrast, is not caught.) Except for the terminating-vs.-nonterminating error distinction, this is consistent with the usual |
Just to make sure we're on the same page, the situation is at least this complicated:
It seems to me that
I'm not proficient at C#. Are there C# statements that continue to the next statement if there is no surrounding
This much is evident from the repro in my OP. But it doesn't really answer these questions from my OP: Does PowerShell search the whole call stack for a try{}catch{}? Are there circumstances aside from a wrapping try{} that results in .ThrowTerminatingError terminating more than just the statement? |
So far, I don't see anything that contradicts my explanation.
Nothing is - by design - consistently terminating vs. nonterminating in PowerShell: The point of the
From what I can tell, yes. Try this simple example (all scripts are assumed to reside in the current location): Script Get-Item -NoSuchParam # provoke statement-terminating error
"With the defaults, I'm pretty sure I won't execute, due to the try / catch on the call stack" Script try {
./t2.ps1
"What are you doing here?"
} catch {
'ouch'
} Running
That is, the statement-terminating error inside
Not that I'm aware of, but do let us know if you find such cases. |
This might be down to semantics, but
The trouble with that approach to answering this question is that it proves what happens in that particular set of circumstances and not much else. I can get a more applicable version of that kind of information from unit testing commands that use |
If you find a scenario that doesn't fit this model (which is based on my experiments), let us know.
I don't know what you mean. |
You model seems to fit. I think you almost have me convinced. :) Thanks for your help @mklement0! |
BrucePay wrote in PowerShell/PowerShell#6286(comment):
|
(Replacing my earlier comment; got confused) To think of this in terms of exceptions is a bit confusing, because the use of exceptions is an implementation detail, given that PowerShell's native "error currency" is error records (
|
Consider the following code:
Invoking
outputs
which seems to be consistent with
.ThrowTerminatingError()
resulting in a "statement-terminating error".On the other hand, invoking
outputs
catch
which indicates that, in this case,.ThrowTerminatingError()
terminates more than just the statement.What is happening with flow of control in the code with the
try{}
block? Does PowerShell search the whole call stack for atry{}catch{}
? Are there circumstances aside from a wrappingtry{}
that results in.ThrowTerminatingError
terminating more than just the statement?FWIW, this arose trying to understand MicrosoftDocs/PowerShell-Docs#1583.
The text was updated successfully, but these errors were encountered: