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

···

--
Posted via http://www.ruby-forum.com/.

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

···

On 5/30/06, Nigel Henbest <tirins.play@gmail.com> 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.

Nigel Henbest wrote:

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.

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

Nikolai Weibull wrote:

···

On 5/30/06, Nigel Henbest <tirins.play@gmail.com> 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.

--
Posted via http://www.ruby-forum.com/\.

Nikolai Weibull wrote:

[begin ... rescue ... end]

So simple!

Thank you.

Don't mention it ;-).

Peace.

  nikolai

···

On 5/30/06, Nigel Henbest <tirins.play@gmail.com> wrote: