DaveWentzel.com            All Things Data

Errors and Error Handling

Information about error-related variables:
get-childitem variable:*error*|format-table Name, Description -auto
 
$ErrorActionPreference
SilentlyContinue  :  non-terminating errors are not displayed on the host at all.  
Continue (default)
Inquire
Stop  :  non-terminating errors behave as terminating errors.  
 
Terminating and NonTerminating Errors
A normal shell error message is not the same as an exception.  Terminating errors stop the execution of the pipeline and result in an exception.  Only exceptions can be trapped.  When an error occurs the shell looks for $ErrorActionPreference to see what it should do.   This works at the script level. 
 
Another method is to use the -EA or -ErrorAction parameter of all cmdlets. 
 
Non-terminating errors allow processing to continue but $Error is still populated.  The error is also displayed on the host's console.  Write-Error generates a non-terminating error.  
 
Trap Keyword
 
the trap keyword defines an error handler.   It only works with terminating errors.  The shell looks for a trap in the current scope (script scope, function scope, etc).  This means an exception inside a function will look for a trap inside that function.  If the shell finds a trap it executes the trap and if the trap has a continue the shell will resume on the line of code after the one that caused the exeption, remaining in that same scope.  This is a bit tricky.  Break , on the other hand, exits the current scope and passes the original exception up to the parent. 
 
Example:
trap {
     write-host "Error connecting to $computer" -fore red
     "$computer" | out-file c:\errors.txt -append
     continue
}
 
$computer = "localhost"
get-wmiobject win32_operatingsystem -comp $computer -ea stop
$computer = "Idontexist"
get-wmiobject win32_operatingsystem -comp $computer -ea stop
$computer = "setmetoacomputerthatdoesexist"
get-wmiobject win32_operatingsystem -comp $computer -ea stop
 
Trap is a PoSH v1 construct.  The preferred method of error handling in PoSH 2.0 is Try/Catch
 
TRY/CATCH
if you use...
 

Catch

{

  
"do something"

  
continue

}

 
you will basically see that a message is output and then the script continues.  But what if you need to handle exact error types?  
 

Catch [some.other.type.of.exception]

{

  
"some other error"

}

Catch [system.exception]

{

  
#this is the most generic error type, look for it last.  

  
"caught a generic exception"

}




So the question becomes...how do you determine the exception class?  Add this to your code and run it, the Exception entry will be the class you need:  

$error|fl * -f




write-error
 
writes an error object and passes it to the pipeline. It is non-terminating.  
 
Error Variables
Error Variable Purpose Example
$? the status of the last operation.  $true is success.  $false is failure

if (!$?){"error occurred"}

$error[0] will contain the same information

$LASTEXITCODE %ERRORLEVEL% from the last windows executable called from this session.   
$ErrorView default is NormalView which shows a few lines of error information.  CategoryView is less confusing for new people since you get one line of error text only.   
 

Add new comment