Ruby style question from a newby: exceptions

Discussion in 'Ruby' started by rj-cole, Nov 17, 2005.

  1. rj-cole

    rj-cole Guest

    Hi,

    Any hints on the best way to catch exceptions from the point of view of
    good style for the following?

    ---
    puts "Create connection..."
    imap = IMAP.new('sslmail.somewhere.com', 993, true)
    puts "Capabilities: cap = #{imap.capability().join(" ")}"
    puts "Authenticate..."
    logged_in = false

    begin
    imap.authenticate('CRAM-MD5', 'myname', 'secret')
    logged_in = true
    rescue Net::IMAP::NoResponseError => e
    puts " Failed, #{e}"
    end

    puts "Try Login..."
    if ! logged_in then
    begin
    imap.login('myname', 'secret')
    logged_in = true
    rescue
    puts " Failed, #{e}"
    end
    end
    ---

    What am I trying to do and what don't I like? Basically I want to try a
    sequence of things until one of them works --- first I want to use the
    authenticate method and if that fails to use the login method. Each of
    these methods indicates failure by raising an exception. I want to log
    each failure (because I'm experimenting with the library at this
    stage). In this example I want to log each failure and on failure I
    want to cacade to an alternative strategy.

    How could this code be better written so that it is easier to read and
    understand the flow of the code? Would it be possible to achieve
    something like the following?

    ---
    exceptions = until_success \
    { imap.authenticate ... },
    { imap.login ... }

    if exceptions.failed then
    puts "Unable to login: #{exceptions.join('\n')}"
    else
    puts "Login successfull. Record of unsuccessfull attempts: \n \
    #{exceptions.join("\n ")}"
    end
    ---

    with a function until_success taking a sequence of blocks as arguments?

    regards,

    Richard.
     
    rj-cole, Nov 17, 2005
    #1
    1. Advertising

  2. Is the following irb session informative?

    irb(main):013:0> c = 0
    irb(main):014:0> begin
    irb(main):015:1* p c
    irb(main):016:1> raise
    irb(main):017:1> rescue
    irb(main):018:1> c += 1
    irb(main):019:1> retry if c < 5
    irb(main):020:1> end
    1
    2
    3
    4
    => nil


    --
    November 17, 2005
    Better late than never.
     
    YANAGAWA Kazuhisa, Nov 17, 2005
    #2
    1. Advertising

  3. rj-cole

    daz Guest

    YANAGAWA Kazuhisa wrote:
    >
    > irb(main):019:1> retry if c < 5



    Hi Richard,

    Here's a variation on that theme:

    #--------------------------------------------------------

    METHOD_Q = [ [:aaa, TypeError],
    [:bbb, NoMethodError],
    [:ccc, ArgumentError],
    [:ddd, NameError],
    ].freeze

    def log(item); puts "LOG: #{item}"; end

    def aaa; 11 + 'Two Two'; end
    def bbb; 12.sort; end
    def ccc; [].each(13) {}; end
    def ddd
    puts "4th attempt"
    # NOCONST # <--- uncomment
    end


    catch:)done) do
    stack = METHOD_Q.dup
    begin
    meth, exc = stack.shift
    unless meth
    log "Tried everything :("
    throw:)done)
    end
    send(meth)
    log "Success at last!"
    rescue exc => e
    log "#{meth} error"
    log "^ #{e.message}"
    retry
    end
    end

    =begin

    LOG: aaa error
    LOG: ^ String can't be coerced into Fixnum
    LOG: bbb error
    LOG: ^ undefined method `sort' for 12:Fixnum
    LOG: ccc error
    LOG: ^ wrong number of arguments (1 for 0)
    4th attempt
    LOG: Success at last!

    =end
    #--------------------------------------------------------


    daz
     
    daz, Nov 17, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    702
    Iain Norman
    Feb 16, 2005
  2. Ahmed Moustafa
    Replies:
    5
    Views:
    30,128
    Chris Smith
    Jul 14, 2004
  3. Paul Miller
    Replies:
    3
    Views:
    1,070
    Alex Martelli
    Nov 12, 2003
  4. Ken Varn
    Replies:
    0
    Views:
    538
    Ken Varn
    Apr 26, 2004
  5. Ruchira Bomiriya

    Newby Ruby Regular Expression Question

    Ruchira Bomiriya, Jun 12, 2008, in forum: Ruby
    Replies:
    4
    Views:
    138
    Ruchira Bomiriya
    Jun 12, 2008
Loading...

Share This Page