# Problems with rescue clause

**URL:** <https://rubytalk.org/t/problems-with-rescue-clause/24166>\
**Category:** ruby-talk\
**Created:** [18 January 2006 23:21 UTC](https://rubytalk.org/t/problems-with-rescue-clause/24166 "2006-01-18T23:21:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Russell\_Fulton](https://avatars.discourse-cdn.com/v4/letter/r/85f322/32.png) [@Russell\_Fulton](https://rubytalk.org/u/Russell_Fulton)\
**Post date:** [18 January 2006 23:21 UTC](https://rubytalk.org/t/problems-with-rescue-clause/24166/1 "2006-01-18T23:21:54Z")

</div>

Hi Folks,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I'm writing a script to check passwords against email accounts,  
so I have written a method:

def tryIMAP (host, usessl, user, pass)

&nbsp;&nbsp;&nbsp;&nbsp;proto = usessl ? "IMAPS" : "IMAP"  
&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imap = Net::IMAP.new(host, usessl ? 993 : 143, usessl)  
&nbsp;&nbsp;&nbsp;rescue SystemCallError  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise "#{proto} failed"  
&nbsp;&nbsp;&nbsp;rescue SocketError  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise "#{proto} failed"  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;imap.login( user, pass)  
&nbsp;&nbsp;&nbsp;&nbsp;imap.logout  
&nbsp;&nbsp;&nbsp;&nbsp;'success'  
&nbsp;&nbsp;rescue Net::IMAP::NoResponseError  
&nbsp;&nbsp;&nbsp;&nbsp;'failure'  
&nbsp;&nbsp;end

end

I use the method like this...

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while proto do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case proto  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when 'IMAPS'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;status = tryIMAP(server, true, user, iD )

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proto = nil  
&nbsp;&nbsp;&nbsp;&nbsp;when 'IMAP'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;status = tryIMAP(server, false, user, iD )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proto = nil  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when 'POP'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;status = tryPOP(server, user, iD )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proto = nil  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proto = nil  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;rescue 'IMAPS failed'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proto = 'IMAP'  
&nbsp;&nbsp;&nbsp;&nbsp;rescue 'IMAP failed'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proto = 'POP'  
&nbsp;&nbsp;&nbsp;&nbsp;rescue 'POP failed'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;servers[host][0] = 'NO'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proto = nil  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;STDERR.print "failed to connect to #{server}\n"  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end # while proto  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "logged in for #{target}\n" if status == 'success'

If I try a server which does not support IMAPS I get:  
class or module required for rescue clause (TypeError)

I'm at a loss to figure out what is wrong. I've googled on the error  
message but have not found anything useful.

BTW I'm new to ruby and I'd welcome criticism of the above code. If  
there are better ways of doing this I'd love to know.

Cheers, Russell

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Wilson\_Bilkovich](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/wilson_bilkovich/32/8247_2.png) [@Wilson\_Bilkovich](https://rubytalk.org/u/Wilson_Bilkovich)\
**Post date:** [18 January 2006 23:37 UTC](https://rubytalk.org/t/problems-with-rescue-clause/24166/2 "2006-01-18T23:37:32Z")

</div>

I would recommend doing:  
raise RuntimeError, "#{proto} failed"  
..instead of:  
raise "#{proto} failed"

Then you can do rescue RuntimeError =\> e  
..and e.message will contain the text of the exception. (e.g. IMAP failed)

Also, you don't really need two identical rescue clauses at the top,  
for the two different kinds of error.  
Personally, I would prevent try\_imap from ever raising an exception,  
and just return a status code. One for success, one for failure, and  
one for an exception. That would greatly clean up the code that calls  
it.

> **···**
>
> On 1/18/06, Russell Fulton \<r.fulton@auckland.ac.nz\> wrote:
> 
> > Hi Folks,  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I'm writing a script to check passwords against email accounts,  
> > so I have written a method:
> > 
> > def tryIMAP (host, usessl, user, pass)
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;proto = usessl ? "IMAPS" : "IMAP"  
> > &nbsp;&nbsp;&nbsp;begin  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imap = Net::IMAP.new(host, usessl ? 993 : 143, usessl)  
> > &nbsp;&nbsp;&nbsp;rescue SystemCallError  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise "#{proto} failed"  
> > &nbsp;&nbsp;&nbsp;rescue SocketError  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise "#{proto} failed"  
> > &nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;begin  
> > &nbsp;&nbsp;&nbsp;&nbsp;imap.login( user, pass)  
> > &nbsp;&nbsp;&nbsp;&nbsp;imap.logout  
> > &nbsp;&nbsp;&nbsp;&nbsp;'success'  
> > &nbsp;&nbsp;rescue Net::IMAP::NoResponseError  
> > &nbsp;&nbsp;&nbsp;&nbsp;'failure'  
> > &nbsp;&nbsp;end
> > 
> > end
> > 
> > I use the method like this...
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while proto do  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case proto  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when 'IMAPS'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;status = tryIMAP(server, true, user, iD )
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proto = nil  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when 'IMAP'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;status = tryIMAP(server, false, user, iD )  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proto = nil  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when 'POP'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;status = tryPOP(server, user, iD )  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proto = nil  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proto = nil  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;STDERR.print "failed to connect to #{server}\n"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end # while proto  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "logged in for #{target}\n" if status == 'success'
> > 
> > If I try a server which does not support IMAPS I get:  
> > class or module required for rescue clause (TypeError)
> > 
> > I'm at a loss to figure out what is wrong. I've googled on the error  
> > message but have not found anything useful.
> > 
> > BTW I'm new to ruby and I'd welcome criticism of the above code. If  
> > there are better ways of doing this I'd love to know.
> > 
> > Cheers, Russell
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Russell\_Fulton](https://avatars.discourse-cdn.com/v4/letter/r/85f322/32.png) [@Russell\_Fulton](https://rubytalk.org/u/Russell_Fulton)\
**Post date:** [19 January 2006 00:11 UTC](https://rubytalk.org/t/problems-with-rescue-clause/24166/3 "2006-01-19T00:11:35Z")

</div>

Thanks Wilson!

Wilson Bilkovich wrote:

> I would recommend doing:  
> raise RuntimeError, "#{proto} failed"  
> ..instead of:  
> raise "#{proto} failed"
> 
> Then you can do rescue RuntimeError =\> e  
> ..and e.message will contain the text of the exception. (e.g. IMAP  
> failed)

I'll give it a try.

> Also, you don't really need two identical rescue clauses at the top,  
> for the two different kinds of error.

Hmmm.... I initially had the two errors on the same rescue but I got  
syntax errors

> Personally, I would prevent try\_imap from ever raising an exception,  
> and just return a status code. One for success, one for failure, and  
> one for an exception. That would greatly clean up the code that calls  
> it.

Yep, that probably would be a better way of doing it but I wanted to  
play with the exceptions:) It is one feature of ruby that I am not  
familiar with from the other languages that I've used.

I'll probabaly cut it out of the final version.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
