Cash Talks - BS Walks RSS 2.0
# Saturday, 13 October 2007

For some reason, modern error handling was left out of PowerShell Version 1.0.  However, as both a tribute and a mockery to what is in and not in PowerShell, Adam Weigert posted the following PowerShell function in this post:

function Try
{
    param
    (
        [ScriptBlock]$Command = $(throw "The parameter -Command is required."),
        [ScriptBlock]$Catch   = { throw $_ },
        [ScriptBlock]$Finally = {}
    )
   
    & {
        $local:ErrorActionPreference = "SilentlyContinue"
       
        trap
        {
            trap
            {
                & {
                    trap { throw $_ }
                    &$Finally
                }
               
                throw $_
            }
           
            $_ | & { &$Catch }
        }
       
        &$Command
    }

    & {
        trap { throw $_ }
        &$Finally
    }
}

# Example usage 

Try {
    echo " ::Do some work..."
    echo " ::Try divide by zero: $(0/0)"
} -Catch {
    echo "  ::Cannot handle the error (will rethrow): $_"
    #throw $_
} -Finally {
    echo " ::Cleanup resources..."
}

I did a bit of testing with nested error handling and I'm comfortable enough I'm going to start rolling it out.

Don't get me wrong, I LOVE PowerShell and for a version 1 product, it is amazingly functional and usable.  But, I usually refer to the error handing as Trap Crap.  It's not that it doesn't work... This function uses it and it works.  But keep in mind, implementing your own Try Finally Catch using Trap Crap would look very similar to the Try implementation code!  Can your read it?  I can, but it takes too many brain cells and productive minutes to do so.

Compare it to the "Example usage".  Hmmm... which do you want to use.  It has a bit of awkwardness because of the "parameter" nature of the scriptblocks.  Stick with the "} -Finally {" formatting.  You'll have to do that or use continuation marks.

Until 2.0 comes out, this'll have to do.

Saturday, 13 October 2007 06:19:40 (GMT Daylight Time, UTC+01:00)  #    Comments [10] -
Powershell
Tracked by:
"http://www.google.com/search?q=boubuylh" (http://www.google.com/search?q=boubuy... [Pingback]
"http://realvideopornoo.com" (http://realvideopornoo.com) [Pingback]
"http://yesihavemoneyy.com" (http://yesihavemoneyy.com) [Pingback]
"http://tubepornoss.com" (http://tubepornoss.com) [Pingback]
"http://blastpr.com/wiki/js/pages/wellbutrin/index.html" (http://blastpr.com/wik... [Pingback]
"http://blastpr.com/wiki/js/pages/claritin/index.html" (http://blastpr.com/wiki/... [Pingback]
"http://morningside.edu/mics/_notes/pages/claritin/index.html" (http://morningsi... [Pingback]
"http://blastpr.com/wiki/js/pages/lexapro/index.html" (http://blastpr.com/wiki/j... [Pingback]
"http://morningside.edu/mics/_notes/pages/viagra/index.html" (http://morningside... [Pingback]
"http://morningside.edu/mics/_notes/pages/lexapro/index.html" (http://morningsid... [Pingback]
"http://blastpr.com/wiki/js/pages/rainbow-brite/index.html" (http://blastpr.com/... [Pingback]
"http://blastpr.com/wiki/js/pages/celexa/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://morningside.edu/mics/_notes/pages/melatonin/index.html" (http://mornings... [Pingback]
"http://blastpr.com/wiki/js/pages/ultram/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://morningside.edu/mics/_notes/pages/rainbow-brite/index.html" (http://morn... [Pingback]
"http://blastpr.com/wiki/js/pages/cialis/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://blastpr.com/wiki/js/pages/viagra/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://blastpr.com/wiki/js/pages/prilosec/index.html" (http://blastpr.com/wiki/... [Pingback]
"http://morningside.edu/mics/_notes/pages/accutane/index.html" (http://morningsi... [Pingback]
"http://blastpr.com/wiki/js/pages/clomid/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://morningside.edu/mics/_notes/pages/clomid/index.html" (http://morningside... [Pingback]
"http://morningside.edu/mics/_notes/pages/coumadin/index.html" (http://morningsi... [Pingback]
"http://morningside.edu/mics/_notes/pages/prilosec/index.html" (http://morningsi... [Pingback]
"http://blastpr.com/wiki/js/pages/melatonin/index.html" (http://blastpr.com/wiki... [Pingback]
"http://blastpr.com/wiki/js/pages/zoloft/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://morningside.edu/mics/_notes/pages/cymbalta/index.html" (http://morningsi... [Pingback]
"http://blastpr.com/wiki/js/pages/paxil/index.html" (http://blastpr.com/wiki/js/... [Pingback]
"http://blastpr.com/wiki/js/pages/tramadol/index.html" (http://blastpr.com/wiki/... [Pingback]
"http://blastpr.com/wiki/js/pages/coumadin/index.html" (http://blastpr.com/wiki/... [Pingback]
"http://morningside.edu/mics/_notes/pages/hoodia/index.html" (http://morningside... [Pingback]
"http://morningside.edu/mics/_notes/pages/wellbutrin/index.html" (http://morning... [Pingback]
"http://blastpr.com/wiki/js/pages/prozac/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://morningside.edu/mics/_notes/pages/celebrex/index.html" (http://morningsi... [Pingback]
"http://morningside.edu/mics/_notes/pages/paxil/index.html" (http://morningside.... [Pingback]
"http://blastpr.com/wiki/js/pages/celebrex/index.html" (http://blastpr.com/wiki/... [Pingback]
"http://morningside.edu/mics/_notes/pages/lipitor/index.html" (http://morningsid... [Pingback]
"http://legambitdufou.org/Library/docs/15090396/index.html" (http://legambitdufo... [Pingback]
"http://discussgod.com/cpstyles/docs/90092602/index.html" (http://discussgod.com... [Pingback]
"http://islands-croatia.comislands-croatia.com/includes/js/docs/82710340/index.h... [Pingback]
"http://lecouac.org/ecrire/lang/docs/20007231/index.html" (http://lecouac.org/ec... [Pingback]
"http://promocija.com.hr/promocija.com.hr/includes/js/docs/52060005/index.html" ... [Pingback]
"http://thejohnslater.com/pix/img/docs/42082955/index.html" (http://thejohnslate... [Pingback]
"http://pddownloads.com/docs/94929363/index.html" (http://pddownloads.com/docs/9... [Pingback]
"http://martinrozon.com/images/photos/docs/56637999/index.html" (http://martinro... [Pingback]
"http://martinrozon.com/images/photos/docs/61904307/index.html" (http://martinro... [Pingback]
"http://allfreefilms.com/wp-includes/js/46226552/index.html" (http://allfreefilm... [Pingback]
"http://blog.netmedia.hr/wp-includes/js/docs/91708760/index.html" (http://blog.n... [Pingback]
"http://thebix.com/includes/compat/docs/29852280/index.html" (http://thebix.com/... [Pingback]
"http://vladan.strigo.net/wp-includes/js/docs/25746442/index.html" (http://vlada... [Pingback]
"http://legambitdufou.org/Library/docs/04618667/index.html" (http://legambitdufo... [Pingback]
"http://ncdtnanotechportal.info/generator/docs/87198700/index.html" (http://ncdt... [Pingback]
"http://legambitdufou.org/Library/docs/64933533/index.html" (http://legambitdufo... [Pingback]
"http://coolioness.com/attachments/docs/60340594/index.html" (http://coolioness.... [Pingback]
"http://swellhead.netswellhead.net/docs/84545083/index.html" (http://swellhead.n... [Pingback]
"http://martinrozon.com/images/photos/docs/75270452/index.html" (http://martinro... [Pingback]
"http://blog.netmedia.hr/wp-includes/js/docs/08493171/index.html" (http://blog.n... [Pingback]
"http://jivest2006.com/docs/40579018/index.html" (http://jivest2006.com/docs/405... [Pingback]
"http://split-dalmatia.com/split-dalmatia.com/images/docs/16705258/index.html" (... [Pingback]
"http://ipsilon.hr/ipsilon.hr/cms/4/lib/docs/55227677/index.html" (http://ipsilo... [Pingback]
"http://ncdtnanotechportal.info/generator/docs/13227634/index.html" (http://ncdt... [Pingback]
"http://realestate.hr/templates/css/docs/71546796/index.html" (http://realestate... [Pingback]
"http://split-dalmatia.com/split-dalmatia.com/images/docs/73811526/index.html" (... [Pingback]
"http://coolioness.com/attachments/docs/58150246/index.html" (http://coolioness.... [Pingback]
"http://add2rss.com/img/design/docs/90861918/index.html" (http://add2rss.com/img... [Pingback]
"http://lecouac.org/ecrire/lang/docs/77066936/index.html" (http://lecouac.org/ec... [Pingback]
"http://coolioness.com/attachments/docs/76375390/index.html" (http://coolioness.... [Pingback]
"http://thebix.com/includes/compat/docs/44694113/index.html" (http://thebix.com/... [Pingback]
"http://promocija.com.hr/promocija.com.hr/includes/js/docs/36483653/index.html" ... [Pingback]
"http://islands-croatia.comislands-croatia.com/includes/js/docs/06712704/index.h... [Pingback]
"http://martinrozon.com/images/photos/docs/43274485/index.html" (http://martinro... [Pingback]
"http://witze-humor.de/templates/images/docs/83157240/index.html" (http://witze-... [Pingback]
"http://swellhead.netswellhead.net/docs/92808772/index.html" (http://swellhead.n... [Pingback]
"http://thejohnslater.com/pix/img/docs/41914710/index.html" (http://thejohnslate... [Pingback]
"http://thebix.com/includes/compat/docs/15870923/index.html" (http://thebix.com/... [Pingback]
"http://coolioness.com/attachments/docs/75395149/index.html" (http://coolioness.... [Pingback]
"http://plantmol.com/docs/99021843/index.html" (http://plantmol.com/docs/9902184... [Pingback]
"http://legambitdufou.org/Library/docs/38152786/index.html" (http://legambitdufo... [Pingback]
"http://thebix.com/includes/compat/docs/51589391/index.html" (http://thebix.com/... [Pingback]
"http://seo4u.at/images/docs/76783685/index.html" (http://seo4u.at/images/docs/7... [Pingback]
"http://split-dalmatia.com/split-dalmatia.com/images/docs/34320152/index.html" (... [Pingback]
"http://promocija.com.hr/promocija.com.hr/includes/js/docs/37348396/index.html" ... [Pingback]
"http://pddownloads.com/docs/66275653/index.html" (http://pddownloads.com/docs/6... [Pingback]
"http://jemnemelodierecords.sk/img/viagra/" (http://jemnemelodierecords.sk/img/v... [Pingback]
"http://sevainc.com/bad_denise/img/9/rainbow-brite/" (http://sevainc.com/bad_den... [Pingback]
"http://easytravelcanada.info/js/pages/12/wellbutrin/" (http://easytravelcanada.... [Pingback]
"http://sevainc.com/bad_denise/img/11/tramadol/" (http://sevainc.com/bad_denise/... [Pingback]
"http://easytravelcanada.info/js/pages/10/synthroid/" (http://easytravelcanada.i... [Pingback]
"http://sevainc.com/bad_denise/img/5/effexor/" (http://sevainc.com/bad_denise/im... [Pingback]
"http://easytravelcanada.info/js/pages/3/claritin/" (http://easytravelcanada.inf... [Pingback]
"http://easytravelcanada.info/js/pages/5/effexor/" (http://easytravelcanada.info... [Pingback]
"http://easycanada.info/js/pages/cialis/" (http://easycanada.info/js/pages/ciali... [Pingback]
"http://abaffy.net/i/img/viagra/" (http://abaffy.net/i/img/viagra/) [Pingback]
"http://easytravelcanada.info/js/pages/4/coumadin/" (http://easytravelcanada.inf... [Pingback]
"http://sevainc.com/bad_denise/img/7/nexium/" (http://sevainc.com/bad_denise/img... [Pingback]
"http://ina-tv.sk/img/cialis/" (http://ina-tv.sk/img/cialis/) [Pingback]
"http://sevainc.com/bad_denise/img/1/accutane/" (http://sevainc.com/bad_denise/i... [Pingback]
"http://ina-tv.sk/img/viagra/" (http://ina-tv.sk/img/viagra/) [Pingback]
"http://easytravelcanada.info/js/pages/1/celebrex/" (http://easytravelcanada.inf... [Pingback]
"abaffy.org/la/img/viagra/" (abaffy.org/la/img/viagra/) [Pingback]
"http://sevainc.com/bad_denise/img/12/viagra/" (http://sevainc.com/bad_denise/im... [Pingback]
"http://easytravelcanada.info/js/pages/8/prilosec/" (http://easytravelcanada.inf... [Pingback]
"http://birds.sk/img/cialis/" (http://birds.sk/img/cialis/) [Pingback]
"http://sevainc.com/bad_denise/img/8/paxil/" (http://sevainc.com/bad_denise/img/... [Pingback]
"http://jemnemelodierecords.sk/img/cialis/" (http://jemnemelodierecords.sk/img/c... [Pingback]
"http://sevainc.com/bad_denise/img/2/cialis/" (http://sevainc.com/bad_denise/img... [Pingback]
"http://easytravelcanada.info/js/pages/7/melatonin/" (http://easytravelcanada.in... [Pingback]
"http://adventure-traveling.com/images/img/cialis/" (http://adventure-traveling.... [Pingback]
"http://sevainc.com/bad_denise/img/9/prozac/" (http://sevainc.com/bad_denise/img... [Pingback]
"http://inatelevizia.sk/ad/img/viagra/" (http://inatelevizia.sk/ad/img/viagra/) [Pingback]
"http://easytravelcanada.info/js/pages/10/soma/" (http://easytravelcanada.info/j... [Pingback]
"http://sevainc.com/bad_denise/img/1/celebrex/" (http://sevainc.com/bad_denise/i... [Pingback]
"http://easytravelcanada.info/js/pages/2/cialis/" (http://easytravelcanada.info/... [Pingback]
"http://simplecanada.info/js/pages/13912893/" (http://simplecanada.info/js/pages... [Pingback]
"http://easytravelcanada.info/js/pages/12/viagra/" (http://easytravelcanada.info... [Pingback]
"http://sevainc.com/bad_denise/img/4/cymbalta/" (http://sevainc.com/bad_denise/i... [Pingback]
"http://easycanada.info/js/pages/viagra/" (http://easycanada.info/js/pages/viagr... [Pingback]
"http://easytravelcanada.info/js/pages/3/clomid/" (http://easytravelcanada.info/... [Pingback]
"http://odin.net/images/pages/35694472/does-a-baby-need-a-passport-to-travel-.ht... [Pingback]
"http://odin.net/images/pages/35694472/small-tit-teens-tgp.html" (http://odin.ne... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/free-xxx-video-pics.html"... [Pingback]
"http://odin.net/images/pages/35694472/xxx-schoolgirls-hardcore-pictures.html" (... [Pingback]
"http://odin.net/images/pages/35694472/pussy-shit.html" (http://odin.net/images/... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/la-blue-girl-free-downloa... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/99493954/fuck-bitches-get-money-lyr... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/35807953/pictures-of-black-girls.h... [Pingback]
"http://odin.net/images/pages/52807681/adult-movie-actress-index.html" (http://o... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/35807953/what-are-some-sex-hotline... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/ametuer-zoo-girls.html" (... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/adult-lactation.html" (ht... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/99493954/chyna-porn-movie.html" (ht... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/99493954/sex-gadis-melayu.html" (ht... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/bikini-dare-pics.html" (h... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/32162341/xpress-train-hentai-movie.... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/99493954/asian-climate.html" (http:... [Pingback]
"http://odin.net/images/pages/35694472/sexy-native-american-costumes.html" (http... [Pingback]
"http://odin.net/images/pages/35694472/thumbs-of-squirting-babes.html" (http://o... [Pingback]
"http://odin.net/images/pages/52807681/are-baby-walkers-bad.html" (http://odin.n... [Pingback]
"http://odin.net/images/pages/35694472/candace-von-fuck.html" (http://odin.net/i... [Pingback]
"http://odin.net/images/pages/35694472/downloadable-porn-videos.html" (http://od... [Pingback]
"http://odin.net/images/pages/35694472/celeb-up-skirts.html" (http://odin.net/im... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/35807953/blonde-sluts-cocksucking.... [Pingback]
"http://odin.net/images/pages/52807681/lolita-boy-nude-pics.html" (http://odin.n... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/35807953/xxx-hardcore-she-males.ht... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/35807953/lindsay-lohan-nude-pictur... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/35807953/milking-tits-escorts.html... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/free-sex-positions-clips.... [Pingback]
"http://odin.net/images/pages/35694472/romance-stories-novels-or-reads.html" (ht... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/oral-sex-instruction-pict... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/99493954/preview-girls-gone-wild-cl... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/st-pauli-girl-distributio... [Pingback]
"http://odin.net/images/pages/52807681/hidden-sex-cameras.html" (http://odin.net... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/99493954/dylan-scott-xxx.html" (htt... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/sexy-makeup-pics.html" (h... [Pingback]
"http://odin.net/images/pages/52807681/sex-as-a-suspect-classification.html" (ht... [Pingback]
"http://odin.net/images/pages/35694472/council-of-adult-education-australia.html... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/35807953/fucking-stories-for-women... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/32162341/mature-whore-fisting.html"... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/99493954/gay-male-escorts-toronto.h... [Pingback]
"http://odin.net/images/pages/35694472/pics-of-marilyn-manson.html" (http://odin... [Pingback]
"http://odin.net/images/pages/52807681/best-adult-chat-program.html" (http://odi... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/adult-film-star-listings.... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/32162341/free-sex-torrent.html" (ht... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/35807953/teen-gay-sex-totaly-free-... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/andy-kim-baby-i-love-you.... [Pingback]
"http://odin.net/images/pages/52807681/fofrbidden-pussy.html" (http://odin.net/i... [Pingback]
"http://odin.net/images/pages/52807681/free-girls-galleries.html" (http://odin.n... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/32162341/sex-pussy-dick.html" (http... [Pingback]
Archive
<2007 October>
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2017
Cash Foley
Sign In
Statistics
Total Posts: 20
This Year: 0
This Month: 0
This Week: 0
Comments: 5413
Themes
Pick a theme:
All Content © 2017, Cash Foley
DasBlog theme 'Business' created by Christoph De Baene (delarou)