sketchucation logo sketchucation
    • Login
    🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Untrapped Error

    Scheduled Pinned Locked Moved Developers' Forum
    5 Posts 3 Posters 187 Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M Offline
      MartinRinehart
      last edited by

      I'm writing a console. You input 2+2 and it outputs 2+2 # 4. Problem is, your input may not be perfect, so a little error trapping is needed.

      In this part of the process() function (line #s added):

      
      59    results = []
      60    for line in lines do
      61        rslt = line.chomp() + ' # '
      62        begin
      63            rslt += eval(line).to_s().chomp()
      64        rescue => e
      65            rslt += e.to_s().chomp()
      66        end
      67    
      68        results.push( qq(rslt) ) 
      69    end # loop over lines
      
      

      I'm thinking that an error at line 63 gets caught and reported at line 65. It usually does. Now, however, I've got this:

      process.jpg

      Any ideas why this isn't caught by the rescue statement? Any ideas on how to catch it?

      Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

      1 Reply Last reply Reply Quote 0
      • T Offline
        todd burch
        last edited by

        From the Pickaxe Book: (Ruby 1.8, page 110)

        @unknownuser said:

        "If you write a rescue clause with no clause parameter list, the parameter defaults to StandardError"

        You got a SyntaxError, which is part of ScriptError, not StandardError.

        Try this:

        rescue Exception => e

        to catch everything.

        1 Reply Last reply Reply Quote 0
        • M Offline
          MartinRinehart
          last edited by

          @unknownuser said:

          rescue Exception => e

          to catch everything.

          I don't want to seem ungrateful, but that upgraded me to Bug Splat. Is there a

          rescue BugSplat => e

          ???

          Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

          1 Reply Last reply Reply Quote 0
          • T Offline
            todd burch
            last edited by

            Do you have access to the pickaxe book? Try ScriptError. Try a stepladder of all the exception subclasses with multiple rescues.

            1 Reply Last reply Reply Quote 0
            • Dan RathbunD Offline
              Dan Rathbun
              last edited by

              @martinrinehart said:

              
              > 64  rescue => e
              > 65    rslt += e.to_s().chomp()
              > 66  end
              > 
              

              Your local reference e receives a pointer to an Exception object from the rescue clause, not a String object.

              You should use the instance method Exception.message to get the exception's string, like this:
              65 rslt << e.message.chomp()

              (As a side-note on Optimization, generally speaking, + and += String concatenation, require Ruby to create at least one extra String object than String append <<. Ruby internally converts a+=b to a=a+b, OR a+='literal' to a=a+'literal', so using += doesn't gain you anything over <<, and may be twice as slow or more, in a loop.)

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post
              Buy SketchPlus
              Buy SUbD
              Buy WrapR
              Buy eBook
              Buy Modelur
              Buy Vertex Tools
              Buy SketchCuisine
              Buy FormFonts

              Advertisement