1

Using the snippet below, I'm trying to find a way to suppress the "Attempted to divide by zero" error, and be left with just the custom error message in $err. I've tried combinations of try/catch, Invoke-Command/Expression, redirecting 2>$null, but in every case the error stream still contains the "attempted to divide by zero" message.

function func1 {
  [CmdletBinding()]
  param()

  func2 -ErrorAction SilentlyContinue -ErrorVariable er
  if ($er.Count -ge 1) {
    Write-Error -Message "Custom error message!"
  }
}

function func2 {
  [CmdletBinding()]
  param()

  1/0
}

func1 -ErrorAction SilentlyContinue -ErrorVariable err
$err

populates $err with two items:

Attempted to divide by zero.
At line:15 char:3
+   1/0
+   ~~~
    + CategoryInfo          : NotSpecified: (:) [], RuntimeException
    + FullyQualifiedErrorId : RuntimeException
 
func1 : Custom error message!
At line:18 char:1
+ func1 -ErrorAction SilentlyContinue -ErrorVariable err
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,func1

Thanks so much for your thoughts!

0

1 Answer 1

1

Unfortunately the solution is somewhat tricky, because the error that occurs in func2 is invariably recorded in the $err variable passed to -ErrorVariable in the func1 invocation, and is already stored there when the func2 call returns.

Therefore you need to manually remove it:

Replace:

if ($er.Count -ge 1) {
  Write-Error -Message "Custom error message!"
}

with:

if ($er.Count -ge 1) {
  if ($PSBoundParameters.ContainsKey('ErrorVariable')) {
    # Remove the most recently added error from the $err variable.
    $PSCmdlet.GetVariableValue($PSBoundParameters['ErrorVariable']).RemoveAt(0)
  }
  Write-Error -Message "Custom error message!"
}

Asides:

  • 1/0 causes a statement-terminating error, whereas the common -ErrorAction parameter only acts on non-terminating errors (to catch terminating errors you need try / catch). However, because this statement-terminating error occurs inside a function, execution inside that function (potentially) continues, and to the caller the error is in effect a non-terminating one.

  • For a comprehensive overview of PowerShell's bewilderingly complex error handling, see GitHub docs issue #1583.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! That's useful... but I don't believe addresses the original problem. If I encapsulate the call to func1 in a try/catch block, it does not help me to suppress the "Attempted to divide my zero" error contained in $err.
In your catch block use throw “<your custom message here” to perform a terminating error and stop the script or use write-error “your message”
@Fitzgery, it turns out that no try / catch was ultimately needed, and the real goal (which I initially missed) was to prevent the 1/0 error from surfacing in the $err variable passed to the outer function call's -ErrorVariable parameter. Doing so requires tricky manual manipulation of this variable, unfortunately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.