Forum: Ruby problems with rescue clause.

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.
9528ea68a2efbf9093b1d41f7f269f36?d=identicon&s=25 Russell Fulton (Guest)
on 2006-01-19 00:58
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
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-01-19 01:15
(Received via mailing list)
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.
9528ea68a2efbf9093b1d41f7f269f36?d=identicon&s=25 Russell Fulton (Guest)
on 2006-01-19 01:47
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.
This topic is locked and can not be replied to.