Ruby exception statement not suppressed within begin-rescue-end block

Discussion in 'Ruby' started by RichardOnRails, Sep 11, 2010.

  1. HI,

    I've got the code below. it creates the output:

    Exception `NameError' at ReformChartNames.rb:8 - uninitialized
    constant DATA
    "__END__" statement or subsequent directory-name missing
    Quitting job

    I thought the "Exception" statement should not be generated because I
    rescued from and errors. So, what's up?

    Thanks in Advance,
    Richard

    === code ===
    [snip]
    $DEBUG = true
    $source_directory_name

    begin
    $source_directory_name =
    DATA.readline # Line # 8
    rescue
    puts %<"__END__" statement or subsequent directory-name missing
    \nQuitting job>
    STDOUT.flush
    exit
    end

    class ReformChartNames
    def initialize
    @source_dir_name = $source_directory_name.chomp
    unless File.directory?(@source_dir_name)
    puts %<ERROR: "%s" is not a valid directory name> %
    [@source_dir_name]
    exit
    end
    end
    end


    rcn = ReformChartNames.new
    puts %<Got ReformChartNames object "rcn"> if $DEBUG
     
    RichardOnRails, Sep 11, 2010
    #1
    1. Advertising

  2. A bare 'rescue' is short for 'rescue StandardError', and catches only
    sunglasses of that. If you want to rescue everything then 'rescue
    Exception', but beware that also catches programmer errors, system
    problems like out of memory etc.

    Google "ruby exception hierarchy" for more.

    To use DATA, put __END__ at the end of your code followed by the data
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Sep 11, 2010
    #2
    1. Advertising

  3. RichardOnRails

    Ryan Davis Guest

    On Sep 10, 2010, at 23:52 , Brian Candler wrote:

    > A bare 'rescue' is short for 'rescue StandardError', and catches only
    > sunglasses of that. If you want to rescue everything then 'rescue
    > Exception', but beware that also catches programmer errors, system
    > problems like out of memory etc.
    >
    > Google "ruby exception hierarchy" for more.
    >
    > To use DATA, put __END__ at the end of your code followed by the data


    sunglasses?

    voice transcription?
     
    Ryan Davis, Sep 11, 2010
    #3
  4. Brian Candler, Sep 11, 2010
    #4
  5. On Sep 11, 2:52 am, Brian Candler <> wrote:
    > A bare 'rescue' is short for 'rescue StandardError', and catches only
    > sunglasses of that. If you want to rescue everything then 'rescue
    > Exception', but beware that also catches programmer errors, system
    > problems like out of memory etc.
    >
    > Google "ruby exception hierarchy" for more.
    >
    > To use DATA, put __END__ at the end of your code followed by the data
    > --
    > Posted viahttp://www.ruby-forum.com/.


    Hi Brian,

    Thanks for your excellent response, not withstanding the sunglasses I
    must have been wearing when I screwed up :) My delay in
    responding, for which I apologize, occurred because I distracted by
    another project.

    Best wishes,
    Richard
     
    RichardOnRails, Sep 27, 2010
    #5
    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. Kurt M. Dresner

    do...end vs. begin..end

    Kurt M. Dresner, Jul 11, 2003, in forum: Ruby
    Replies:
    3
    Views:
    116
  2. Richard Lionheart
    Replies:
    15
    Views:
    200
    Richard Lionheart
    Jul 18, 2004
  3. Harold Hausman

    Question about begin, rescue, end.

    Harold Hausman, Nov 18, 2005, in forum: Ruby
    Replies:
    9
    Views:
    182
    Stefan Kaes
    Nov 18, 2005
  4. J. Cooper
    Replies:
    3
    Views:
    119
    J. Cooper
    Apr 1, 2008
  5. Iñaki Baz Castillo

    How to use "retry" into two deep begin/rescue/end ?

    Iñaki Baz Castillo, Apr 3, 2008, in forum: Ruby
    Replies:
    4
    Views:
    100
    Iñaki Baz Castillo
    Apr 3, 2008
Loading...

Share This Page