# Problems with rescue clause

**URL:** <https://www.ruby-forum.com/t/problems-with-rescue-clause/52777>\
**Category:** Ruby\
**Created:** [January 19, 2006, 12:58am UTC](https://www.ruby-forum.com/t/problems-with-rescue-clause/52777 "2006-01-19T00:58:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [January 19, 2006, 12:58am UTC](https://www.ruby-forum.com/t/problems-with-rescue-clause/52777/1 "2006-01-19T00:58:09Z")

</div>

Hi Folks,  
I’m writing a script to check passwords against email accounts,  
so I have written a method:

def tryIMAP (host, usessl, user, pass)

```
proto = usessl ? "IMAPS" : "IMAP"

```

begin  
imap = Net::IMAP.new(host, usessl ? 993 : 143, usessl)  
rescue SystemCallError  
raise “#{proto} failed”  
rescue SocketError  
raise “#{proto} failed”  
end  
begin  
imap.login( user, pass)  
imap.logout  
‘success’  
rescue Net::IMAP::NoResponseError  
‘failure’  
end

end

I use the method like this…

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

	  proto = nil
	when 'IMAP'
          status = tryIMAP(server, false, user, iD )
	  proto = nil
        when 'POP'
          status = tryPOP(server, user, iD )
          proto = nil
        else
          proto = nil
        end
	rescue 'IMAPS failed'
	  proto = 'IMAP'
	rescue 'IMAP failed'
	  proto = 'POP'
	rescue 'POP failed'
	  servers[host][0] = 'NO'
	  proto = nil
	  STDERR.print "failed to connect to #{server}\n"
	end
    end # while proto
    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

---

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [January 19, 2006, 1:15am UTC](https://www.ruby-forum.com/t/problems-with-rescue-clause/52777/2 "2006-01-19T01:15:18Z")

</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.

---

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [January 19, 2006, 1:47am UTC](https://www.ruby-forum.com/t/problems-with-rescue-clause/52777/3 "2006-01-19T01:47:51Z")

</div>

Thanks Wilson!

Wilson B. 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.
