Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Try/catch does not seem to have an effect

I am new to powershell, and I am trying to add error handling via try/catch statements, but they don't seem to actually be catching the error. This is powershell v2 CP3.

$objComputer = $objResult.Properties; $strComputerName = $objComputer.name write-host "Checking machine: " $strComputerName  try {     $colItems = get-wmiobject -class "Win32_PhysicalMemory" -namespace "root\CIMV2" -computername $strComputerName -Credential $credentials     foreach ($objItem in $colItems)      {         write-host "Bank Label: " $objItem.BankLabel         write-host "Capacity: " ($objItem.Capacity / 1024 / 1024)         write-host "Caption: " $objItem.Caption         write-host "Creation Class Name: " $objItem.CreationClassName               write-host     } } Catch  {     write-host "Failed to get data from machine (Error:"  $_.Exception.Message ")"     write-host } finally  { }   

When it fails to contact a specific machine, I get this in console, and not my clean catch message:

Get-WmiObject : The RPC server is
unavailable. (Exception from HRESULT:
0x800706BA) At Z:\7.0 Intern
Programvare\Powershell\Get memory of
all computers in AD.ps1:25 char:34
+ $colItems = get-wmiobject <<<< -class "Win32_PhysicalMemory"
-namespace "root\CIMV2" -computername $strComputerName -Credential
$credentials
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject],
COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

like image 712
EKS Avatar asked Jul 17 '09 09:07

EKS


People also ask

Does Try Catch affect performance?

In general, wrapping your Java code with try/catch blocks doesn't have a significant performance impact on your applications. Only when exceptions actually occur is there a negative performance impact, which is due to the lookup the JVM must perform to locate the proper handler for the exception.

Does try catch stop errors?

The try... catch construct allows to handle runtime errors. It literally allows to “try” running the code and “catch” errors that may occur in it.

What happens if try catch block is not used?

1 Answer. Explanation: If try catch block is not used the exception thrown by the program will be uncaught hence will result into error(s).

Is try catch better than if else?

'try-catch' is more time consuming than 'if-else'. 'if-else' communicate between the data provided to the program and the condition. 'try-catch' communicate between the data with the conditions and the system.


1 Answers

I was able to duplicate your result when trying to run a remote WMI query. The exception thrown is not caught by the Try/Catch, nor will a Trap catch it, since it is not a "terminating error". In PowerShell, there are terminating errors and non-terminating errors . It appears that Try/Catch/Finally and Trap only works with terminating errors.

It is logged to the $error automatic variable and you can test for these type of non-terminating errors by looking at the $? automatic variable, which will let you know if the last operation succeeded ($true) or failed ($false).

From the appearance of the error generated, it appears that the error is returned and not wrapped in a catchable exception. Below is a trace of the error generated.

PS C:\scripts\PowerShell> Trace-Command -Name errorrecord  -Expression {Get-WmiObject win32_bios -ComputerName HostThatIsNotThere}  -PSHost DEBUG: InternalCommand Information: 0 :  Constructor Enter Ctor Microsoft.PowerShell.Commands.GetWmiObjectCommand: 25857563 DEBUG: InternalCommand Information: 0 :  Constructor Leave Ctor Microsoft.PowerShell.Commands.GetWmiObjectCommand: 25857563 DEBUG: ErrorRecord Information: 0 :  Constructor Enter Ctor System.Management.Automation.ErrorRecord: 19621801 exception = System.Runtime.InteropServices.COMException (0x800706BA): The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)    at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)    at System.Management.ManagementScope.InitializeGuts(Object o)    at System.Management.ManagementScope.Initialize()    at System.Management.ManagementObjectSearcher.Initialize()    at System.Management.ManagementObjectSearcher.Get()    at Microsoft.PowerShell.Commands.GetWmiObjectCommand.BeginProcessing() errorId = GetWMICOMException errorCategory = InvalidOperation targetObject = DEBUG: ErrorRecord Information: 0 :  Constructor Leave Ctor System.Management.Automation.ErrorRecord: 19621801 

A work around for your code could be:

try {     $colItems = get-wmiobject -class "Win32_PhysicalMemory" -namespace "root\CIMV2" -computername $strComputerName -Credential $credentials     if ($?)     {       foreach ($objItem in $colItems)        {           write-host "Bank Label: " $objItem.BankLabel           write-host "Capacity: " ($objItem.Capacity / 1024 / 1024)           write-host "Caption: " $objItem.Caption           write-host "Creation Class Name: " $objItem.CreationClassName                 write-host       }     }     else     {        throw $error[0].Exception     } 
like image 198
Steven Murawski Avatar answered Sep 24 '22 17:09

Steven Murawski