Exception handling

I have a(nother) question concerning exception handling.

My code retrieves stock data from yahoo like this.

require ‘yahoofinance’

tickers = File.new("/home/nigels/financials/symbols")
tickers.each_line do |ticker|
quotes = YahooFinance::get_standard_quotes( ticker )
quotes.each do |symbol, qt|

end
end

Occasionally, yahoo will drop requests which results in timeout
exceptions. I would like to “catch” the exception and simply continue
with the next ticker.

Any ideas.

Thanks for your help.

Nigel

On 5/30/06, Nigel H. [email protected] wrote:

I have a(nother) question concerning exception handling.

Occasionally, yahoo will drop requests which results in timeout
exceptions. I would like to “catch” the exception and simply continue
with the next ticker.

require ‘yahoofinance’

tickers = File.new(“/home/nigels/financials/symbols”)
tickers.each_line do |ticker|
begin
quotes = YahooFinance::get_standard_quotes( ticker )
quotes.each do |symbol, qt|

end
rescue
# Do nothing, just continue with the next ticker.
end
end

Peace.

nikolai

Nikolai W. wrote:

On 5/30/06, Nigel H. [email protected] wrote:

I have a(nother) question concerning exception handling.

Occasionally, yahoo will drop requests which results in timeout
exceptions. I would like to “catch” the exception and simply continue
with the next ticker.

require ‘yahoofinance’

tickers = File.new(“/home/nigels/financials/symbols”)
tickers.each_line do |ticker|
begin
quotes = YahooFinance::get_standard_quotes( ticker )
quotes.each do |symbol, qt|

end
rescue
# Do nothing, just continue with the next ticker.
end
end

Peace.

nikolai

So simple!

Thank you.

On 5/30/06, Nigel H. [email protected] wrote:

Nikolai W. wrote:
[begin … rescue … end]
So simple!

Thank you.

Don’t mention it ;-).

Peace.

nikolai

Nigel H. wrote:

Occasionally, yahoo will drop requests which results in timeout
exceptions. I would like to “catch” the exception and simply continue
with the next ticker.

You just need normal exception handling, right?

tickers = File.new("/home/nigels/financials/symbols")
tickers.each_line do |ticker|
begin
YahooFinance.get_standard_quotes(ticker).each do |symbol, qt|
# …
end
rescue NameOfError
# do some nifty stuff
end
end

Since you’re handling the exceptions within the block, the loop isn’t
interrupted, but will proceed with the next ticker.

I recently proposed a more elegant syntax for rescuing exceptions within
do/end blocks:

tickers.each_line do |ticker|
YahooFinance.get_standard_quotes(ticker).each do |symbol, qt|
# …
end
rescue NameOfError
# …
end

though that’s still up for discussion (I hope)

Cheers,
Daniel