Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > begin/rescue not behaving how I want

Reply
Thread Tools

begin/rescue not behaving how I want

 
 
J. Cooper
Guest
Posts: n/a
 
      04-01-2008
So I have some code that routinely checks for email in a cron job. Most
of the time it works great. However, sometimes it gets a timeout error,
which is fine; it happens. The thing is, I don't want it to clog up my
logs when it does, because it's not an exception I'm worried about. So I
tried wrapping the code in question in a begin/rescue block that just
exits on exception, thinking that then it the error would be "caught"
and it wouldn't have to bug me about it... but it continues to do so.
How do I get what I want to achieve here?

(The relevant code, if needed):
begin
Net:OP3.start('foo', 110, 'bar', 'foobar') do |pop|
if pop.mails.empty?
exit
else
# blah blah etc.
end
end
rescue
exit
end
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
J. Cooper
Guest
Posts: n/a
 
      04-01-2008
Oh, and thanks of course!!

(Didn't mean to be rude; got distracted pasting code)

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

 
Reply With Quote
 
 
 
 
Arlen Cuss
Guest
Posts: n/a
 
      04-01-2008
[Note: parts of this message were removed to make it a legal post.]

Check out these two cases:

celtic@sohma:~$ irb
>> begin

?> raise TimeoutError, "data"
>> rescue
>> puts "Test?"
>> end

(irb):2:in `irb_binding': data (Timeout::Error)
from /usr/lib/ruby/1.8/irb/workspace.rb:52:in `irb_binding'
from /usr/lib/ruby/1.8/irb/workspace.rb:52

celtic@sohma:~$ irb
>> begin

?> raise TimeoutError, "data"
>> rescue TimeoutError => t
>> puts "Error with #{t}."
>> end

Error with data.
=> nil
>>


In the first one, the exception is raised (and irb doesn't catch it and
dies). In the second one, we explicitly catch TimeoutError, and it's fine.
Why so?

TimeoutError isn't a "StandardError", which is the default thing that
`rescue' tries to rescue when you leave it blank!

>> TimeoutError.superclass

=> Interrupt
>> Interrupt.superclass

=> SignalException
>> SignalException.superclass

=> Exception
>>


Notice that it goes TimeoutError < Interrupt < SignalException < Exception.
Whereas, for example...

>> ZeroDivisionError.superclass

=> StandardError
>> StandardError.superclass

=> Exception
>>


A ZeroDivisionError is of superclass StandardError (which is then an
Exception). So it would get caught.

To catch all cases, try rescue Exception -- or REALLY all cases with rescue
Object. (if you're crazy.) But of course, try not to catch anything you're
not expecting - that's how unexpected errors turn up. Instead, perhaps just
rescue Timeout (if it's the only error you expect). That way other errors
you don't expect aren't silently thrown away [leading to frustration for you
when you try to debug!]

Cheers,
Arlen.
On Tue, Apr 1, 2008 at 3:49 PM, J. Cooper <(E-Mail Removed)> wrote:

> So I have some code that routinely checks for email in a cron job. Most
> of the time it works great. However, sometimes it gets a timeout error,
> which is fine; it happens. The thing is, I don't want it to clog up my
> logs when it does, because it's not an exception I'm worried about. So I
> tried wrapping the code in question in a begin/rescue block that just
> exits on exception, thinking that then it the error would be "caught"
> and it wouldn't have to bug me about it... but it continues to do so.
> How do I get what I want to achieve here?
>
> (The relevant code, if needed):
> begin
> Net:OP3.start('foo', 110, 'bar', 'foobar') do |pop|
> if pop.mails.empty?
> exit
> else
> # blah blah etc.
> end
> end
> rescue
> exit
> end
> --
> Posted via http://www.ruby-forum.com/.
>
>


 
Reply With Quote
 
J. Cooper
Guest
Posts: n/a
 
      04-01-2008
Arlen Cuss wrote:
> Check out these two cases:



You are a gentleman and a scholar! Thank you!!

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Java String.replaceAll() not behaving as expected William Krick Java 4 12-18-2008 03:08 PM
list not behaving mark | r HTML 2 09-04-2004 06:34 PM
ThreadAbortException not behaving properly? Steve - DND ASP .Net 3 05-20-2004 04:22 AM
writeChars not behaving as expected Ken Kafieh Java 7 05-18-2004 11:58 PM



Advertisments