OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Powershell finally block executed unexpectedly early

  • Thread starter Thread starter Silex
  • Start date Start date
S

Silex

Guest
With the following script:

Code:
$ErrorActionPreference = 'Stop'

try {
  qweqweqwe
}
catch {
  Write-Error $_ #-ErrorAction Continue
}
finally {
  Read-Host -Prompt 'Press enter to continue'
}

Read-Host -Prompt 'What'

Here is the output (notice how the "finally" is executed before the "Write-Error":

Code:
Press enter to continue: 

C:\Demo.ps1 : The term 'qweqweqwe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path 
is correct and try again.
At C:\Demo.ps1:7 char:3
+   Write-Error $_ #-ErrorAction Continue
+   ~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Demo.ps1

Here's the expected output when I uncomment "-ErrorAction Continue":

Code:
C:\Demo.ps1 : The term 'qweqweqwe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path 
is correct and try again.
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Demo.ps1
 
Press enter to continue: 

What:

I can only assume that powershell, when it sees he'll "Write-Error" and exit, starts something like "ok but before that let's execute the finally block before".

Is this behavior documented somewhere?
<p>With the following script:</p>
<pre><code>$ErrorActionPreference = 'Stop'

try {
qweqweqwe
}
catch {
Write-Error $_ #-ErrorAction Continue
}
finally {
Read-Host -Prompt 'Press enter to continue'
}

Read-Host -Prompt 'What'
</code></pre>
<p>Here is the output (notice how the "finally" is executed before the "Write-Error":</p>
<pre><code>Press enter to continue:

C:\Demo.ps1 : The term 'qweqweqwe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path
is correct and try again.
At C:\Demo.ps1:7 char:3
+ Write-Error $_ #-ErrorAction Continue
+ ~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Demo.ps1
</code></pre>
<p>Here's the expected output when I uncomment "-ErrorAction Continue":</p>
<pre><code>C:\Demo.ps1 : The term 'qweqweqwe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path
is correct and try again.
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Demo.ps1

Press enter to continue:

What:
</code></pre>
<p>I can only assume that powershell, when it sees he'll "Write-Error" and exit, starts something like "ok but before that let's execute the finally block before".</p>
<p>Is this behavior documented somewhere?</p>
Continue reading...
 
Top