# Exception handling

**URL:** <https://www.ruby-forum.com/t/exception-handling/61985>\
**Category:** Ruby\
**Created:** [May 30, 2006, 4:09pm UTC](https://www.ruby-forum.com/t/exception-handling/61985 "2006-05-30T16:09:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nigelh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ce73a5/32.png) [@nigelh](https://www.ruby-forum.com/u/nigelh)\
**Post date:** [May 30, 2006, 4:09pm UTC](https://www.ruby-forum.com/t/exception-handling/61985/1 "2006-05-30T16:09:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nigelh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ce73a5/32.png) [@nigelh](https://www.ruby-forum.com/u/nigelh)\
**Post date:** [May 30, 2006, 4:17pm UTC](https://www.ruby-forum.com/t/exception-handling/61985/2 "2006-05-30T16:17:18Z")

</div>

On 5/30/06, Nigel H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![nigelh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ce73a5/32.png) [@nigelh](https://www.ruby-forum.com/u/nigelh)\
**Post date:** [May 30, 2006, 5:15pm UTC](https://www.ruby-forum.com/t/exception-handling/61985/3 "2006-05-30T17:15:35Z")

</div>

Nikolai W. wrote:

> On 5/30/06, Nigel H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![nigelh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ce73a5/32.png) [@nigelh](https://www.ruby-forum.com/u/nigelh)\
**Post date:** [May 30, 2006, 5:19pm UTC](https://www.ruby-forum.com/t/exception-handling/61985/4 "2006-05-30T17:19:38Z")

</div>

On 5/30/06, Nigel H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

Don’t mention it ;-).

Peace.

nikolai

---

<div class="post-metadata">

**Author:** ![nigelh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ce73a5/32.png) [@nigelh](https://www.ruby-forum.com/u/nigelh)\
**Post date:** [May 30, 2006, 4:27pm UTC](https://www.ruby-forum.com/t/exception-handling/61985/5 "2006-05-30T16:27:01Z")

</div>

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
