I'm trying to work out how to specify the Catch values for PowerShell V2 CTP3's Try/Catch stuff.
Here's a simple divide by zero error that I'm trying to catch:
<#.SYNOPSIS Demonstrates try/catch/finally in V2.DESCRIPTION Shows a simple example of the try/catch/finally syntax introduced into PowerShell CTP3. The script divies by zero which creates an exception. The powershell parser is smart enougy 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 CTP3.LINK http://www.pshscripts.blogspot.com#>### Start of script### Try something that fails$one=1$zero=0Try { $one/$zero }catch ("Attemped to divide by zero") {"WOW"}Catch { "Caught in a catch" $err=$Error[0] $err $err |Format-List *}Finally {"All done with trying and catching"}
The first catch gives an error. But so does pretty much everything I try. How do I catch this error? And more importantly, where are this get documented??
See this help file: about_Try_Catch_Finally
$one=1$zero=0
try{ $one/$zero}
catch [System.DivideByZeroException]{ #"The first Catch script block handles System.DivideByZeroException exceptions" "Attempted to divide by zero"}
catch { "the second Catch script block handles any non-specific exceptions"} finally { "All done with trying and catching"}
So what is different about dividing by 0 instead of a variable that contains zero?
PS> try {1/$zero} catch [System.DivideByZeroException] {"divide by zero" ; continue} catch {"some other problem"}divide by zero
PS> try {1/0} catch [System.DivideByZeroException] {"divide by zero" ; continue} catch {"some other problem"}Attempted to divide by zero.At line:1 char:8+ try {1/ <<<< 0} catch [System.DivideByZeroException] {"divide by zero" ; continue} catch {"some other problem"} + CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : RuntimeException
Bruce Payette explains it in his book, Windows PowerShell in Action (p.257):
PS (5) > 1/$nullAttempted to divide by zero.At line:1 char:3+ 1/$ <<<< null
The example here uses 1/$null. The reason for doing this instead of simply 1/0 is because the PowerShell interpreter does something called constant expression folding.
It looks at expressions that contain only constant values. When it sees one, it evaluates that expression once at compile time so it doesn’t have to waste time doing it again at runtime.
This means that impossible expressions, such as division by zero, are caught and treated asparsing errors. Parsing errors can’t be caught and don’t get logged when they’re enteredinteractively, so they don’t make for a good example. (If one script calls another script and that script has one of these errors, the calling script can catch it, but the script being parsed cannot.)
Brilliant I have the book, I clearly have not remembered it all, thanks for that. Now what about:
try {dir q:} catch {"Problem with dir command"; continue}ortrap {"Problem with dir command"; continue} ; dir q:
Were q is a drive that does not exist. Why can I not catch this error?
Because the error is not a terminating error, try/catch/trap works on terminating errors.
If you want to supress the error message:
PS > dir q: -errorAction silentlyContinue
or use the short notation:
PS > dir q: -ea 0
HTH
This is very good info, but I'm lost when it comes to dealing with COM object exceptions inside PowerShell. Some are rather simple, for example, I've registered a DLL (e.g. KiXtart.DLL which comes with KiXtart 4.60). I can invoke the interface as an object in VBScript and JScript fine. It works. But from within PowerShell I get nothing.
Set objKix = CreateObject("KiXtart.Application") ' works fine
$objKix = new-object -com KiXtart.Application # returns nothing
How should I trap the exception in this case? Or (I would rather) use Try/Catch to find out more about the failure? I'm a bit new to PowerShell so please forgive me for asking boneheaded questions?