(Simple?) Why is my rescue clause not catching an exception?

Discussion in 'Ruby' started by Wes Gamble, Nov 20, 2006.

  1. Wes Gamble

    Wes Gamble Guest

    All,

    I'm using the Net::HTTP module to fetch resources from the Internet in a
    Rails application.

    I have a method that does the HTTP request and this action is enclosed
    in a begin/rescue/end block. However, when I just did a request and the
    Internet connectivity went down for a second, the call to
    "http.request(req)" failed and I didn't see my rescue block get
    executed. Instead, it appears that the original exception (from
    timeout.rb) just bubbles all of the way up the stack and my rescue
    doesn't seem to catch it.

    Is this because the call to http.request is inside of it's own block and
    I would need to place a rescue clause in there?

    Here's the guts of my method:

    begin
    if (theURL.instance_of?(URI::HTTP))
    req = Net::HTTP::Get.new(theURL.request_uri())
    res = Net::HTTP.start(theURL.host, theURL.port) {|http|
    http.request(req)
    }
    end
    rescue
    raise("Unable to get page - please check the URL")
    end

    Thanks,
    Wes

    --
    Posted via http://www.ruby-forum.com/.
    Wes Gamble, Nov 20, 2006
    #1
    1. Advertising

  2. On 20.11.2006 18:34, Wes Gamble wrote:
    > All,
    >
    > I'm using the Net::HTTP module to fetch resources from the Internet in a
    > Rails application.
    >
    > I have a method that does the HTTP request and this action is enclosed
    > in a begin/rescue/end block. However, when I just did a request and the
    > Internet connectivity went down for a second, the call to
    > "http.request(req)" failed and I didn't see my rescue block get
    > executed. Instead, it appears that the original exception (from
    > timeout.rb) just bubbles all of the way up the stack and my rescue
    > doesn't seem to catch it.
    >
    > Is this because the call to http.request is inside of it's own block and
    > I would need to place a rescue clause in there?
    >
    > Here's the guts of my method:
    >
    > begin
    > if (theURL.instance_of?(URI::HTTP))
    > req = Net::HTTP::Get.new(theURL.request_uri())
    > res = Net::HTTP.start(theURL.host, theURL.port) {|http|
    > http.request(req)
    > }
    > end
    > rescue
    > raise("Unable to get page - please check the URL")
    > end
    >
    > Thanks,
    > Wes
    >


    rescue catches StandardError by default only and the exception thrown by
    Net::HTTP is likely some other type not inheriting standard error.

    >> StandardError.ancestors

    => [StandardError, Exception, Object, Kernel]

    You need

    rescue Exception

    Kind regards

    robert
    Robert Klemme, Nov 20, 2006
    #2
    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. Chris Miller
    Replies:
    4
    Views:
    13,333
    Chris Smith
    Nov 22, 2003
  2. Derek Schuff

    except clause not catching IndexError

    Derek Schuff, Feb 22, 2006, in forum: Python
    Replies:
    7
    Views:
    543
    Sion Arrowsmith
    Feb 23, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,816
    Smokey Grindel
    Dec 2, 2006
  4. W0rtex Indigo
    Replies:
    3
    Views:
    132
    W0rtex Indigo
    Jun 7, 2007
  5. Patrick Doyle

    rescue clause

    Patrick Doyle, Oct 10, 2008, in forum: Ruby
    Replies:
    3
    Views:
    102
    Patrick Doyle
    Oct 10, 2008
Loading...

Share This Page