Sunday 23 November 2008

Catch-Error.ps1

<#
.SYNOPSIS
    Demonstrates try/catch/finally in V2
.DESCRIPTION
    Shows a simple example of the try/catch/finally syntax
    introduced into PowerShell V2 CTP3 . The script divides by
    zero which creates an exception. The PowerShell parser
    is smart enough to recognise any attempt to divide 
    by "0" and therefore does not generate the run time error.
.NOTES
    File Name  : Show-TryCatchFinally.ps1
    Author     : Thomas Lee - tfl@psp.co.uk
    Requires   : PowerShell V2
.LINK
   http://pshscripts.blogspot.com/2008/11/catch-errorps1.html
#>

##
# Start of script
##

# Try something that fails
$one=1
$zero=0
Try
  { 
      $one/$zero
  }
Catch{
 'Caught in a catch block'
 $Error[0]
}
Finally {'All done with trying and catching'
}

No comments: