error handling

Discussion in 'Ruby' started by Phidippus, Apr 1, 2004.

  1. Phidippus

    Phidippus Guest

    I'm evaluating a function at various values in a for loop. The
    function works most of the times (99% of parameter range I am
    investigating), but at some values, it gives error like below and
    crushes. I guess something wrong with sqrt function. I'm not going to
    attempt to show the function here because it is 1.5Mb. If I evaluate
    the exact same function in Maple at the value it crushed in Ruby, it
    behaves fine.

    Is there any way that when the (sqrt) function crushes, it gives some
    sort of error value and go on to evaluating at the next value without
    crushing the program? I don't want to manually restart the program at
    the next value each time the program crushes. In R, there is "try"
    function that can used for this kind of situation.

    Thank you.

    #####
    /usr/lib/ruby/1.6/complex.rb:82:in `initialize': stack level too deep
    (SystemStackError)
    from /usr/lib/ruby/1.6/complex.rb:63:in `new'
    from /usr/lib/ruby/1.6/complex.rb:63:in `Complex'
    from /usr/lib/ruby/1.6/complex.rb:130:in `/'
    from /usr/lib/ruby/1.6/mathn.rb:244:in `sqrt'
    from /usr/lib/ruby/1.6/mathn.rb:244:in `sqrt'
    from /usr/lib/ruby/1.6/mathn.rb:244:in `sqrt'
    from /usr/lib/ruby/1.6/mathn.rb:244:in `sqrt'
    from /usr/lib/ruby/1.6/mathn.rb:244:in `sqrt'
    ... 1461 levels...
    from rubyHopf0.rb:17:in `each'
    from rubyHopf0.rb:17
    from rubyHopf0.rb:16:in `each'
    from rubyHopf0.rb:16
     
    Phidippus, Apr 1, 2004
    #1
    1. Advertising

  2. Phidippus

    Mark Hubbart Guest

    On Apr 1, 2004, at 8:29 AM, Phidippus wrote:

    > I'm evaluating a function at various values in a for loop. The
    > function works most of the times (99% of parameter range I am
    > investigating), but at some values, it gives error like below and
    > crushes. I guess something wrong with sqrt function. I'm not going to
    > attempt to show the function here because it is 1.5Mb. If I evaluate
    > the exact same function in Maple at the value it crushed in Ruby, it
    > behaves fine.
    >
    > Is there any way that when the (sqrt) function crushes, it gives some
    > sort of error value and go on to evaluating at the next value without
    > crushing the program? I don't want to manually restart the program at
    > the next value each time the program crushes. In R, there is "try"
    > function that can used for this kind of situation.
    >
    > Thank you.
    >
    > #####
    > /usr/lib/ruby/1.6/complex.rb:82:in `initialize': stack level too deep
    > (SystemStackError)
    > from /usr/lib/ruby/1.6/complex.rb:63:in `new'
    > from /usr/lib/ruby/1.6/complex.rb:63:in `Complex'
    > from /usr/lib/ruby/1.6/complex.rb:130:in `/'
    > from /usr/lib/ruby/1.6/mathn.rb:244:in `sqrt'
    > from /usr/lib/ruby/1.6/mathn.rb:244:in `sqrt'
    > from /usr/lib/ruby/1.6/mathn.rb:244:in `sqrt'
    > from /usr/lib/ruby/1.6/mathn.rb:244:in `sqrt'
    > from /usr/lib/ruby/1.6/mathn.rb:244:in `sqrt'
    > ... 1461 levels...
    > from rubyHopf0.rb:17:in `each'
    > from rubyHopf0.rb:17
    > from rubyHopf0.rb:16:in `each'
    > from rubyHopf0.rb:16


    This looks like a bug... sqrt() is recursing too deeply here, 1461
    levels seems a bit excessive. What values are causing it to dump like
    that? Could you give a specific example?

    Normally, you could catch an error like this:

    begin
    0 / 0
    rescue
    puts "no dividing by zero in integer division!"
    end

    ... but that doesn't work for me with a SystemStackError. I guess those
    are always fatal errors.

    --Mark
     
    Mark Hubbart, Apr 1, 2004
    #2
    1. Advertising

  3. "Mark Hubbart" <> schrieb im Newsbeitrag
    news:...
    >
    > On Apr 1, 2004, at 8:29 AM, Phidippus wrote:
    >
    > > I'm evaluating a function at various values in a for loop. The
    > > function works most of the times (99% of parameter range I am
    > > investigating), but at some values, it gives error like below and
    > > crushes. I guess something wrong with sqrt function. I'm not going to
    > > attempt to show the function here because it is 1.5Mb.


    Are you serious? A single function with 1.5MB source code?

    > If I evaluate
    > > the exact same function in Maple at the value it crushed in Ruby, it
    > > behaves fine.


    Well, you can't compare Ruby with Maple on this: Maple is a math system
    built to efficiency solve mathematical problems. Ruby is more of a general
    purpose language and especially not very good at recursion. The stack
    nesting problem surfaces every now and then.

    AFAIK there is a compiler switch that you can employ during building of Ruby
    that will increase the stack size. Alternatively you can implement sqrt as
    iterative function yourself.

    > > Is there any way that when the (sqrt) function crushes, it gives some
    > > sort of error value and go on to evaluating at the next value without
    > > crushing the program? I don't want to manually restart the program at
    > > the next value each time the program crushes. In R, there is "try"
    > > function that can used for this kind of situation.


    See below.

    > > Thank you.
    > >
    > > #####
    > > /usr/lib/ruby/1.6/complex.rb:82:in `initialize': stack level too deep
    > > (SystemStackError)
    > > from /usr/lib/ruby/1.6/complex.rb:63:in `new'
    > > from /usr/lib/ruby/1.6/complex.rb:63:in `Complex'
    > > from /usr/lib/ruby/1.6/complex.rb:130:in `/'
    > > from /usr/lib/ruby/1.6/mathn.rb:244:in `sqrt'
    > > from /usr/lib/ruby/1.6/mathn.rb:244:in `sqrt'
    > > from /usr/lib/ruby/1.6/mathn.rb:244:in `sqrt'
    > > from /usr/lib/ruby/1.6/mathn.rb:244:in `sqrt'
    > > from /usr/lib/ruby/1.6/mathn.rb:244:in `sqrt'
    > > ... 1461 levels...
    > > from rubyHopf0.rb:17:in `each'
    > > from rubyHopf0.rb:17
    > > from rubyHopf0.rb:16:in `each'
    > > from rubyHopf0.rb:16

    >
    > This looks like a bug... sqrt() is recursing too deeply here, 1461
    > levels seems a bit excessive. What values are causing it to dump like
    > that? Could you give a specific example?


    I guess they are quite big so sqrt needs to many steps to identify the
    square root.

    > Normally, you could catch an error like this:
    >
    > begin
    > 0 / 0
    > rescue
    > puts "no dividing by zero in integer division!"
    > end
    >
    > .. but that doesn't work for me with a SystemStackError. I guess those
    > are always fatal errors.


    Works perfectly for me:

    irb(main):036:0> def rec; rec; end
    => nil
    irb(main):037:0> begin
    irb(main):038:1* rec
    irb(main):039:1> rescue SystemStackError => e
    irb(main):040:1> puts "Caught it: #{e.inspect}"
    irb(main):041:1> end
    Caught it: #<SystemStackError: stack level too deep>
    => nil

    Regards

    robert
     
    Robert Klemme, Apr 1, 2004
    #3
  4. Phidippus

    Mark Hubbart Guest

    On Apr 1, 2004, at 11:44 AM, Robert Klemme wrote:

    >
    > "Mark Hubbart" <> schrieb im Newsbeitrag
    > news:...
    >
    > [...]
    >
    >> This looks like a bug... sqrt() is recursing too deeply here, 1461
    >> levels seems a bit excessive. What values are causing it to dump like
    >> that? Could you give a specific example?

    >
    > I guess they are quite big so sqrt needs to many steps to identify the
    > square root.


    Oh. I hadn't realized that sqrt was supposed to recurse that much at
    all... even using two 60 digit numbers to create a Complex number to
    take the square root of, I couldn't get sqrt to cause me any problems,
    using 1.6.8...

    >
    >> Normally, you could catch an error like this:
    >>
    >> begin
    >> 0 / 0
    >> rescue
    >> puts "no dividing by zero in integer division!"
    >> end
    >>
    >> .. but that doesn't work for me with a SystemStackError. I guess those
    >> are always fatal errors.

    >
    > Works perfectly for me:
    >
    > irb(main):036:0> def rec; rec; end
    > => nil
    > irb(main):037:0> begin
    > irb(main):038:1* rec
    > irb(main):039:1> rescue SystemStackError => e
    > irb(main):040:1> puts "Caught it: #{e.inspect}"
    > irb(main):041:1> end
    > Caught it: #<SystemStackError: stack level too deep>
    > => nil


    Okay, that's kind of surprising. It works for me too, but only if I
    specify the error type like in your code... I tried it the other time
    in the same way as my example code above, and it failed. I thought that
    if you didn't specify the error type, it would catch anything;
    apparently that's not the case.

    --Mark
     
    Mark Hubbart, Apr 2, 2004
    #4
  5. "Mark Hubbart" <> schrieb im Newsbeitrag
    news:...
    >
    > On Apr 1, 2004, at 11:44 AM, Robert Klemme wrote:
    >
    > >
    > > "Mark Hubbart" <> schrieb im Newsbeitrag
    > > news:...
    > >
    > > [...]
    > >
    > >> This looks like a bug... sqrt() is recursing too deeply here, 1461
    > >> levels seems a bit excessive. What values are causing it to dump like
    > >> that? Could you give a specific example?

    > >
    > > I guess they are quite big so sqrt needs to many steps to identify the
    > > square root.

    >
    > Oh. I hadn't realized that sqrt was supposed to recurse that much at
    > all... even using two 60 digit numbers to create a Complex number to
    > take the square root of, I couldn't get sqrt to cause me any problems,
    > using 1.6.8...


    That was just a wild guess. I'd normally not expect sqrt to be written as
    recursive function at all. But the stacktrace seemed to indicate that.

    > >> Normally, you could catch an error like this:
    > >>
    > >> begin
    > >> 0 / 0
    > >> rescue
    > >> puts "no dividing by zero in integer division!"
    > >> end
    > >>
    > >> .. but that doesn't work for me with a SystemStackError. I guess

    those
    > >> are always fatal errors.

    > >
    > > Works perfectly for me:
    > >
    > > irb(main):036:0> def rec; rec; end
    > > => nil
    > > irb(main):037:0> begin
    > > irb(main):038:1* rec
    > > irb(main):039:1> rescue SystemStackError => e
    > > irb(main):040:1> puts "Caught it: #{e.inspect}"
    > > irb(main):041:1> end
    > > Caught it: #<SystemStackError: stack level too deep>
    > > => nil

    >
    > Okay, that's kind of surprising. It works for me too, but only if I
    > specify the error type like in your code... I tried it the other time
    > in the same way as my example code above, and it failed. I thought that
    > if you didn't specify the error type, it would catch anything;


    I thought that once, too. But I got wiser in the meantime. :)

    > apparently that's not the case.


    "If you write a rescue clause with no parameter list, the parameter
    defaults to StandardError."
    http://www.rubycentral.com/book/tut_exceptions.html

    Regards

    robert
     
    Robert Klemme, Apr 2, 2004
    #5
  6. Phidippus

    Phidippus Guest

    I'm having hard time pinpointing where the error is generated since
    there are so many places sqrt are used...

    (Phidippus) wrote in message news:<>...
    > I'm evaluating a function at various values in a for loop. The
    > function works most of the times (99% of parameter range I am
    > investigating), but at some values, it gives error like below and
    > crushes. I guess something wrong with sqrt function. I'm not going to
    > attempt to show the function here because it is 1.5Mb. If I evaluate
    > the exact same function in Maple at the value it crushed in Ruby, it
    > behaves fine.
    >
    > Is there any way that when the (sqrt) function crushes, it gives some
    > sort of error value and go on to evaluating at the next value without
    > crushing the program? I don't want to manually restart the program at
    > the next value each time the program crushes. In R, there is "try"
    > function that can used for this kind of situation.
    >
    > Thank you.
    >
    > #####
    > /usr/lib/ruby/1.6/complex.rb:82:in `initialize': stack level too deep
    > (SystemStackError)
    > from /usr/lib/ruby/1.6/complex.rb:63:in `new'
    > from /usr/lib/ruby/1.6/complex.rb:63:in `Complex'
    > from /usr/lib/ruby/1.6/complex.rb:130:in `/'
    > from /usr/lib/ruby/1.6/mathn.rb:244:in `sqrt'
    > from /usr/lib/ruby/1.6/mathn.rb:244:in `sqrt'
    > from /usr/lib/ruby/1.6/mathn.rb:244:in `sqrt'
    > from /usr/lib/ruby/1.6/mathn.rb:244:in `sqrt'
    > from /usr/lib/ruby/1.6/mathn.rb:244:in `sqrt'
    > ... 1461 levels...
    > from rubyHopf0.rb:17:in `each'
    > from rubyHopf0.rb:17
    > from rubyHopf0.rb:16:in `each'
    > from rubyHopf0.rb:16
     
    Phidippus, Apr 3, 2004
    #6
  7. On Sat, 03 Apr 2004 09:47:00 -0800, Phidippus wrote:
    > I'm having hard time pinpointing where the error is generated since
    > there are so many places sqrt are used...
    >


    I don't know if overloading Math.sqrt will help you?

    Then you could log every time #sqrt is invoked.

    --
    Simon Strandgaard
     
    Simon Strandgaard, Apr 3, 2004
    #7
  8. Phidippus

    Phidippus Guest

    Yes, one single function is 1.5Mb. It is rediculous, but true. I tried
    whether the exact same function also works in R, and it worked fine.
    But R is also developed for numerical computations, so it is probably
    not a fair comparison for Ruby, I suppose.

    "Robert Klemme" <> wrote in message news:<c4hrb3$2i812u$-berlin.de>...
    > "Mark Hubbart" <> schrieb im Newsbeitrag
    > news:...
    > >
    > > On Apr 1, 2004, at 8:29 AM, Phidippus wrote:
    > >
    > > > I'm evaluating a function at various values in a for loop. The
    > > > function works most of the times (99% of parameter range I am
    > > > investigating), but at some values, it gives error like below and
    > > > crushes. I guess something wrong with sqrt function. I'm not going to
    > > > attempt to show the function here because it is 1.5Mb.

    >
    > Are you serious? A single function with 1.5MB source code?
    >
    > > If I evaluate
    > > > the exact same function in Maple at the value it crushed in Ruby, it
    > > > behaves fine.

    >
    > Well, you can't compare Ruby with Maple on this: Maple is a math system
    > built to efficiency solve mathematical problems. Ruby is more of a general
    > purpose language and especially not very good at recursion. The stack
    > nesting problem surfaces every now and then.
    >
    > AFAIK there is a compiler switch that you can employ during building of Ruby
    > that will increase the stack size. Alternatively you can implement sqrt as
    > iterative function yourself.
    >
    > > > Is there any way that when the (sqrt) function crushes, it gives some
    > > > sort of error value and go on to evaluating at the next value without
    > > > crushing the program? I don't want to manually restart the program at
    > > > the next value each time the program crushes. In R, there is "try"
    > > > function that can used for this kind of situation.

    >
    > See below.
    >
    > > > Thank you.
    > > >
    > > > #####
    > > > /usr/lib/ruby/1.6/complex.rb:82:in `initialize': stack level too deep
    > > > (SystemStackError)
    > > > from /usr/lib/ruby/1.6/complex.rb:63:in `new'
    > > > from /usr/lib/ruby/1.6/complex.rb:63:in `Complex'
    > > > from /usr/lib/ruby/1.6/complex.rb:130:in `/'
    > > > from /usr/lib/ruby/1.6/mathn.rb:244:in `sqrt'
    > > > from /usr/lib/ruby/1.6/mathn.rb:244:in `sqrt'
    > > > from /usr/lib/ruby/1.6/mathn.rb:244:in `sqrt'
    > > > from /usr/lib/ruby/1.6/mathn.rb:244:in `sqrt'
    > > > from /usr/lib/ruby/1.6/mathn.rb:244:in `sqrt'
    > > > ... 1461 levels...
    > > > from rubyHopf0.rb:17:in `each'
    > > > from rubyHopf0.rb:17
    > > > from rubyHopf0.rb:16:in `each'
    > > > from rubyHopf0.rb:16

    > >
    > > This looks like a bug... sqrt() is recursing too deeply here, 1461
    > > levels seems a bit excessive. What values are causing it to dump like
    > > that? Could you give a specific example?

    >
    > I guess they are quite big so sqrt needs to many steps to identify the
    > square root.
    >
    > > Normally, you could catch an error like this:
    > >
    > > begin
    > > 0 / 0
    > > rescue
    > > puts "no dividing by zero in integer division!"
    > > end
    > >
    > > .. but that doesn't work for me with a SystemStackError. I guess those
    > > are always fatal errors.

    >
    > Works perfectly for me:
    >
    > irb(main):036:0> def rec; rec; end
    > => nil
    > irb(main):037:0> begin
    > irb(main):038:1* rec
    > irb(main):039:1> rescue SystemStackError => e
    > irb(main):040:1> puts "Caught it: #{e.inspect}"
    > irb(main):041:1> end
    > Caught it: #<SystemStackError: stack level too deep>
    > => nil
    >
    > Regards
    >
    > robert
     
    Phidippus, Apr 3, 2004
    #8
  9. "Phidippus" <> schrieb im Newsbeitrag
    news:...
    > Yes, one single function is 1.5Mb. It is rediculous, but true. I tried
    > whether the exact same function also works in R, and it worked fine.
    > But R is also developed for numerical computations, so it is probably
    > not a fair comparison for Ruby, I suppose.


    Whatever language is used, 1.5 GB source code for a single function is
    definitely too much. Some systems might gracefully deal with it, but what
    about human maintainers? IMHO heavy refactoring is indicated.

    Regards

    robert
     
    Robert Klemme, Apr 8, 2004
    #9
    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. Kenneth Keeley

    Error Handling

    Kenneth Keeley, Jul 1, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    388
    Kenneth Keeley
    Jul 1, 2003
  2. VSK
    Replies:
    1
    Views:
    805
    Alvin Bruney
    Jul 29, 2003
  3. Mark Tarver
    Replies:
    22
    Views:
    1,336
    J Kenneth King
    Apr 26, 2009
  4. Peter
    Replies:
    34
    Views:
    1,975
    James Kanze
    Oct 17, 2009
  5. Iñaki Baz Castillo
    Replies:
    1
    Views:
    200
    Iñaki Baz Castillo
    Apr 15, 2008
Loading...

Share This Page