Sunday, 23 November 2008

Catch-Error.ps1

#Requires -Version 2
#
Show-TryCatchFinally.ps1
#
Demonstrates try/catch/finally in V2
#
First, try something that will work
Try {
$I=1
}
Catch {
"Caught a problem in try 1"
}
Finally {
"All done with 1st try"
}

# Now try something that fails
$one=1
#$zero=0
Try { $one/$zero}
Catch {
"Caught a problem in try 2"}
Finally {"All done with second try"}

This script produces the following output:
PS C:\foo> . 'C:\Users\tfl\AppData\Local\Temp\Untitled14.ps1'
All done with 1st try
Caught a problem in try 2
All done with second try

0 comments: