JDMils_Interact
Enthusiast
Enthusiast

Ignore Try..Catch on terminating errors

I'm very sorry for this basic question and I MUST be missing something obvious. I cannot get a terminating error located within a Try..Catch to be ignored and for processing to continue.

 

$ErrorActionPreference = "Ignore"
Try
    {
    Write-Host "Try this...."
    Get-Item -Location "C:\Temp" -ErrorAction Ignore
    Write-Host "Error ignored"
    }

Catch
    {
    Write-Host "Try-Catch activated"
    }
Finally
    {
    Write-Host "Finally activated"
    }

 

Basically I want the terminating error "Get-Item -Location "C:\Temp" -ErrorAction Ignore" to be ignored and processing to continue. In my testing, the Catch block is always processed.

My PS version:

$PSVersionTable

Name                           Value                                                                                                                                                                                                                            
----                           -----                                                                                                                                                                                                                            
PSVersion                      5.1.14393.6343                                                                                                                                                                                                                   
PSEdition                      Desktop                                                                                                                                                                                                                          
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                                                                                                          
BuildVersion                   10.0.14393.6343                                                                                                                                                                                                                  
CLRVersion                     4.0.30319.42000                                                                                                                                                                                                                  
WSManStackVersion              3.0                                                                                                                                                                                                                              
PSRemotingProtocolVersion      2.3                                                                                                                                                                                                                              
SerializationVersion           1.1.0.1             
Reply
0 Kudos
LucD
Leadership
Leadership

With -ErrorAction Ignore you are in fact disabling the terminating exception.
So your code will never end in the Catch block.

If the cmdlet(s) you want to capture does/do not have a terminating exception, you must add -ErrorAction Stop.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
JDMils_Interact
Enthusiast
Enthusiast

That's what I thought. When I run the code, it actually ALWAYS enters the Catch block as shown here:

 

 

PS C:\Users\jmilano\Documents\PowerShell_Scripts\Datastores> C:\Users\jmilano\Documents\PowerShell_Scripts\TestTryCatch.ps1
Try this....
Try-Catch activated
Finally activated

 

 

I have the Try..Catch to capture all major terminating errors, however I have one command which i do NOT want to terminate on, I just want the code to continue running. The problem is that the code ALWAYS enters the Catch block and I cannot figger out why I.

Note: Get-Item does not have a location parameter so this should trigger an error which I want to ignore. This is for testing purposes atm.

Even testing from the command line, ErrorAction doesn't work:

PS C:\Users\jmilano\Documents\PowerShell_Scripts> get-item -location "c:\temp" -ErrorAction Continue
Get-Item : A parameter cannot be found that matches parameter name 'location'.
At line:1 char:10
+ get-item -location "c:\temp" -ErrorAction Continue
+          ~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-Item], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetItemCommand

PS C:\Users\jmilano\Documents\PowerShell_Scripts> get-item -location "c:\temp" -ErrorAction Ignore
Get-Item : A parameter cannot be found that matches parameter name 'location'.
At line:1 char:10
+ get-item -location "c:\temp" -ErrorAction Ignore
+          ~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-Item], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetItemCommand

PS C:\Users\jmilano\Documents\PowerShell_Scripts> get-item -location "c:\temp" -ErrorAction Inquire
Get-Item : A parameter cannot be found that matches parameter name 'location'.
At line:1 char:10
+ get-item -location "c:\temp" -ErrorAction Inquire
+          ~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-Item], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetItemCommand

PS C:\Users\jmilano\Documents\PowerShell_Scripts> get-item -location "c:\temp" -ErrorAction SilentlyContinue
Get-Item : A parameter cannot be found that matches parameter name 'location'.
At line:1 char:10
+ get-item -location "c:\temp" -ErrorAction SilentlyContinue
+          ~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-Item], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetItemCommand
Reply
0 Kudos
LucD
Leadership
Leadership

The reason the Catch block is activated is because the Get-Item cmdlet does not have a Location parameter.
I suspect you mean to use the parameter Path there.

Also the Finally block is always executed, independent if there was an error or not.

So my code

Try { 
    Write-Host "Try this...."
    Get-Item -Path "C:\Temp" -ErrorAction Ignore
    Write-Host "Error ignored"
}
Catch {
    Write-Host "Try-Catch activated"
}
Finally {
    Write-Host "Finally activated"
}

Correctly shows

Try this....

    Directory: C:\

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           14-May-23    13:11                Temp
Error ignored
Finally activated

And if use a non-existing folder, thus provoking an error, I get

Try this....
Error ignored
Finally activated

No folder was found/listed


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
JDMils_Interact
Enthusiast
Enthusiast

I was using Get-Item -Location "C:\Temp" because I wanted to create a terminating error, then using the -ErrorAction Ignore, I was hoping the code would ignore the error and continue running.

This is just my way of testing.

 

Reply
0 Kudos
LucD
Leadership
Leadership

A syntax error is not a terminating exception.
That error is captured during the syntax check before the PS engine even starts executing the code.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
xuyenphungngoc
Contributor
Contributor

encounter the same issue

Reply
0 Kudos
LucD
Leadership
Leadership

There is no issue, it works as designed.
The test is incorrect, see my previous explanation of the difference between syntax checking and runtime exceptions.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos