Please feel free to provide feedback or file bugs here.

Errors that terminate script due to -ErrorAction Stop do not show correct line number

PS > type .\error.ps1
[Cmdletbinding()]
param()

"hello"
"world"

1/0

PS > .\error.ps1
hello
world
Attempted to divide by zero.
At D:\junk\error.ps1:10 char:5
+ 1/0
+ ~~~
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException

PS > $ErrorActionPreference = "Stop"
PS > .\error.ps1
hello
world
D:\junk\error.ps1 : Attempted to divide by zero.
At line:1 char:1
+ .\error.ps1
+ ~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [error.ps1], RuntimeException
+ FullyQualifiedErrorId : RuntimeException,error.ps1

Note incorrect line number being reported on second invocation when ErrorActionPreference equals "Stop"

This can also be reproduced by putting "$ErrorActionPreference = "Stop" at line 3 of the script.

I always set ErrorActionPreference to Stop at the start of my scripts, and also have ErrorActionPreference set to "Stop" always in my environment for improved robustness as I write scripts, As a result this issue is very problematic. The correct line number can be obtained by digging into $Error but that is a pain.

This started happening somewhere around the v5 release. Prior to that the correct line number was always reported. This can also be reproduced by using -ErrorAction Stop at a particular invocation, although that is a less compelling scenario.

PSVersion 5.1.18272.1000

3 votes
Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)

We’ll send you updates on this idea

Lyle Corbin (MSFT) shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

0 comments

Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
Submitting...

Feedback and Knowledge Base