Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - handling errors in a script

Reply
 
Old 11-29-2007   #1 (permalink)
Darren Mar-Elia


 
 

handling errors in a script

If I'm calling a cmdlet in a PSh script, what is the best way to trap for
errors in the cmdlet? In other words, I call the cmdlet then do a bunch of
things afterwards. I'd like to script to bail out if the cmdlet throws an
exception. I'm sure this is easy, but just looking for the "right" approach.

Thanks!

Darren


My System SpecsSystem Spec
Old 11-29-2007   #2 (permalink)
Kiron


 
 

Re: handling errors in a script

Place the trap before the call to the Cmdlet. '$_' holds the error information. Use exit in the trap to exit the script. If the error is a non-terminating error set the Cmdlet's -ErrorAction parameter to Stop or 1:

Try these sample scripts:

-< test1.ps1 >-
trap {
'Exception trapped!'
$_
exit
}
# non-terminating error, ignored by trap
Get-Content nosuchdir

# terminating error
Get-Date 'Monday'

"last line, should not be displayed"
-< end >-

# - - - - - - - - #

-< test2.ps1 >-
trap {
'Exception trapped!'
$_
exit
}
# set -ErrorAction to Stop
# on non-terminating error
Get-Content nosuchdir -ea 1

# terminating error
Get-Date 'Monday'

"last line, should not be displayed"
-< end >-

--
Kiron
My System SpecsSystem Spec
Old 11-29-2007   #3 (permalink)
Kirk Munro


 
 

Re: handling errors in a script

If you are used to try-catch-finally functionality and would prefer to use
that in your scripts, you should read this blog post:

http://weblogs.asp.net/adweigert/arc...s-to-life.aspx

--
Kirk Munro
Poshoholic
http://poshoholic.com


"Kiron" <Kiron@xxxxxx> wrote in message
news:C686D8FD-E416-4DD7-BFEF-2C78AAF1789B@xxxxxx
Quote:

> Place the trap before the call to the Cmdlet. '$_' holds the error
> information. Use exit in the trap to exit the script. If the error is a
> non-terminating error set the Cmdlet's -ErrorAction parameter to Stop or
> 1:
>
> Try these sample scripts:
>
> -< test1.ps1 >-
> trap {
> 'Exception trapped!'
> $_
> exit
> }
> # non-terminating error, ignored by trap
> Get-Content nosuchdir
>
> # terminating error
> Get-Date 'Monday'
>
> "last line, should not be displayed"
> -< end >-
>
> # - - - - - - - - #
>
> -< test2.ps1 >-
> trap {
> 'Exception trapped!'
> $_
> exit
> }
> # set -ErrorAction to Stop
> # on non-terminating error
> Get-Content nosuchdir -ea 1
>
> # terminating error
> Get-Date 'Monday'
>
> "last line, should not be displayed"
> -< end >-
>
> --
> Kiron

My System SpecsSystem Spec
Old 11-29-2007   #4 (permalink)
Darren Mar-Elia


 
 

Re: handling errors in a script

Thanks Kirk and Kiron. Very useful info.


Darren

"Kirk Munro" <sorry@xxxxxx> wrote in message
news:Osye$MsMIHA.292@xxxxxx
Quote:

> If you are used to try-catch-finally functionality and would prefer to use
> that in your scripts, you should read this blog post:
>
> http://weblogs.asp.net/adweigert/arc...s-to-life.aspx
>
> --
> Kirk Munro
> Poshoholic
> http://poshoholic.com
>
>
> "Kiron" <Kiron@xxxxxx> wrote in message
> news:C686D8FD-E416-4DD7-BFEF-2C78AAF1789B@xxxxxx
Quote:

>> Place the trap before the call to the Cmdlet. '$_' holds the error
>> information. Use exit in the trap to exit the script. If the error is a
>> non-terminating error set the Cmdlet's -ErrorAction parameter to Stop or
>> 1:
>>
>> Try these sample scripts:
>>
>> -< test1.ps1 >-
>> trap {
>> 'Exception trapped!'
>> $_
>> exit
>> }
>> # non-terminating error, ignored by trap
>> Get-Content nosuchdir
>>
>> # terminating error
>> Get-Date 'Monday'
>>
>> "last line, should not be displayed"
>> -< end >-
>>
>> # - - - - - - - - #
>>
>> -< test2.ps1 >-
>> trap {
>> 'Exception trapped!'
>> $_
>> exit
>> }
>> # set -ErrorAction to Stop
>> # on non-terminating error
>> Get-Content nosuchdir -ea 1
>>
>> # terminating error
>> Get-Date 'Monday'
>>
>> "last line, should not be displayed"
>> -< end >-
>>
>> --
>> Kiron
>
>
My System SpecsSystem Spec
Old 11-29-2007   #5 (permalink)
Keith Hill [MVP]


 
 

Re: handling errors in a script

"Darren Mar-Elia" <dmanonymous@xxxxxx> wrote in message
news:2B668D38-8B7F-434F-B23A-2F0967D18CDB@xxxxxx
Quote:

> If I'm calling a cmdlet in a PSh script, what is the best way to trap for
> errors in the cmdlet? In other words, I call the cmdlet then do a bunch of
> things afterwards. I'd like to script to bail out if the cmdlet throws an
> exception. I'm sure this is easy, but just looking for the "right"
> approach.
>
It sounds like to me that you don't need to trap errors. You just want to
enforce that if the cmdlet errors, the error is a terminating error instead
of a non-terminating error. To do this, use the ubiqitous
parameter -ErrorAction and set it to Stop e.g.:

Get-ChildItem someNameThatDoesntExist -ErrorAction Stop

This will cause script execution to jump to the previous trap handler in
scope and on up the call stack. If none exists the script will exit
automatically with that error.

Another option, if you want this behavior for all cmdlet calls within a
certain scope, is to set $ErrorActionPreference = 'Stop' before the cmdlet
invocations.

--
Keith

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Strategy for handling parameters to a script PowerShell
Script errors Vista mail
script errors Vista Games
IE Script Errors Vista General
Handling Errors from COM components PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46