Trapping error not caught by rescue Exception -- How to?

Discussion in 'Ruby' started by Richard, Dec 17, 2006.

  1. Richard

    Richard Guest

    Hi,

    The fourth data line in the program below hangs rather than submitting
    to the rescue clause. How can I trap that kind of error?

    I'm running WinXP-Pro/SP2, Ruby 1.8.2-15, Rails 1.1.6, MySQL
    5.0.15-nt, SciTE 1.59

    TIA,
    Richard

    === code start ===
    begin
    DATA.each do |line|
    var, expr, excess = line.chomp.split(/=/, 3)
    var.strip!; expr.strip!
    puts "expr=" + expr + ", expr.class=" +expr.class.to_s
    puts "eval(expr)=" + eval(expr).to_s
    end
    rescue Exception => eMsg
    puts "ERROR: " + eMsg
    retry
    end

    __END__
    a = 5**2 =# 25
    b =date =# undefined local variable or method `date' for
    main:Object
    c = y =# undefined local variable or method `y' for main:Object
    c =`date` =# (ruby hangs; Ctrl-C kills it)
    === code end ===
     
    Richard, Dec 17, 2006
    #1
    1. Advertisements

  2. Ruby doesn't hang when I run that code.

    This is the output:
    [email protected]:~$ ruby /tmp/tmp.rb
    expr=5**2, expr.class=String
    eval(expr)=25
    expr=date, expr.class=String
    ERROR: undefined local variable or method `date' for main:Object
    ERROR: undefined method `strip!' for nil:NilClass
    expr=y, expr.class=String
    ERROR: undefined local variable or method `y' for main:Object
    expr=`date`, expr.class=String
    eval(expr)=Sat Dec 16 22:01:37 EST 2006
     
    Daniel Finnie, Dec 17, 2006
    #2
    1. Advertisements

  3. Richard

    Richard Guest

    Hi Daniel,

    Thanks for your results. I suspect you were running on Uni*. I was
    running in SciTE over WindowsXP.

    When I ran the program in a Command window, I saw what the problem is:
    in Windows, the date command waits for keyboard input specifying a new
    system date. That wasn't apparent when I was running under SciTE.

    So, all's well in my Ruby sandbox.

    Regards,
    Richard
     
    Richard, Dec 17, 2006
    #3
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.