d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Powershell
Add Reply New Topic New Poll
Member
Posts: 12,348
Joined: Jun 30 2007
Gold: 1,573.00
Trader: Trusted
Apr 13 2014 02:30pm
I have a few powershell assignments i need to do was wondering if you guys could look over them for me see if im doing it right

Create a script that uses a function to request a stock quote from webservicex.net. Include a trap for system exceptions to show a graceful error message if one arises. Use read-host to ask the user of the script for a stock symbol to look up and verify you were given one before trying to look it up.

Code
function get-stockQuote([Parameter(Mandatory=$true)][string]$symbol)
{
$URI = "http://www.webservicex.net/stockquote.asmx?WSDL"
$stockProxy = New-WebServiceProxy -uri $URI
$quote = [xml]$stockProxy.getquote($symbol)
$stock = $quote.StockQuotes.stock
$stock|ft -AutoSize
} #end Get-stockQuote

trap [System.SystemException]
{
$_.exception
continue
} # end of exception trap

while ($true) {
$symbol= read-host -Prompt "Please enter a stock symbol: "
if (-not $symbol) {
exit
}
get-stockQuote($symbol)
}


Member
Posts: 8,112
Joined: Sep 23 2006
Gold: 3,558.23
Apr 14 2014 08:59pm
Looks fine to me. And it functions too!! Only thing you're missing is a "graceful" error message. You can test it by disconnecting from the network and then running the script to see what your error message will look like.

Personally, I would have done it a bit different, using a try/catch instead of trap, but I'm not sure if that counts as using a "trap" to catch the error.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Apr 14 2014 09:14pm
Quote (Qord @ Apr 14 2014 09:59pm)
Looks fine to me. And it functions too!! Only thing you're missing is a "graceful" error message. You can test it by disconnecting from the network and then running the script to see what your error message will look like.

Personally, I would have done it a bit different, using a try/catch instead of trap, but I'm not sure if that counts as using a "trap" to catch the error.


traps are globally scoped, whereas try/catch/finally have local scope to the statements you have wrapped. You can look at a trap as being just a massive "catch" that wraps the entire script.
Member
Posts: 8,112
Joined: Sep 23 2006
Gold: 3,558.23
Apr 14 2014 09:20pm
I haven't used a trap in anything I've written so far. Most of it's pretty small though, so it might not be necessary for those. I'm not even a fan of try/catch, but my boss says to use it so I am. I'm still newb enough to not worry too much about error handling, but I'm seeing the value of it as my tools get larger and more complex.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Apr 14 2014 09:24pm
Quote (Qord @ Apr 14 2014 10:20pm)
I haven't used a trap in anything I've written so far. Most of it's pretty small though, so it might not be necessary for those. I'm not even a fan of try/catch, but my boss says to use it so I am. I'm still newb enough to not worry too much about error handling, but I'm seeing the value of it as my tools get larger and more complex.


They both have their places.

For statements which may go wrong in localized areas of your script, you will want to wrap them in try/catch and do your error handling as needed. However, bigger scripts may have specific errors which can occur througout different parts of the scripts. Instead of having multiple try/catchs doing the exact same thing, you can define a trap, which for ALL exceptions that are trapped, no matter where in the script it occurs, will be piped through that one trap block.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll