Forum: Ruby exception handling

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5e47d4036f04fac27729804410c3f349?d=identicon&s=25 Nigel Henbest (Guest)
on 2006-05-30 16:09
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
3807680355002d0d5e8314a97333587e?d=identicon&s=25 Nikolai Weibull (Guest)
on 2006-05-30 16:17
(Received via mailing list)
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
5da4c52f43677f395aff5bde775593c2?d=identicon&s=25 Daniel Schierbeck (dasch)
on 2006-05-30 16:27
(Received via mailing list)
Nigel Henbest 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
5e47d4036f04fac27729804410c3f349?d=identicon&s=25 Nigel Henbest (Guest)
on 2006-05-30 17:15
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.
3807680355002d0d5e8314a97333587e?d=identicon&s=25 Nikolai Weibull (Guest)
on 2006-05-30 17:19
(Received via mailing list)
On 5/30/06, Nigel Henbest <tirins.play@gmail.com> wrote:
> Nikolai Weibull wrote:
[begin ... rescue ... end]
> So simple!
>
> Thank you.

Don't mention it ;-).

Peace.

  nikolai
This topic is locked and can not be replied to.