Hiding app.run in begin-rescue block: pros & cons?

Discussion in 'Ruby' started by Richard Lionheart, Jul 15, 2004.

  1. Hi All,

    Upon asking a friend to review my code, he suggested that many people hide
    app.run in a begin-rescue block. I assume one might code:

    begin
    app.run
    rescue => err
    puts "Application failed:", err
    end

    (which I got from Hal Fulton's "The Ruby Way")

    What are the pros and cons of this approach?

    Regards,
    Richard


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.719 / Virus Database: 475 - Release Date: 7/13/2004
    Richard Lionheart, Jul 15, 2004
    #1
    1. Advertising

  2. Hi All,

    I apologize for not testing the two approaches myself before posting the
    question. My take on this now is:

    Use begin-rescue when code is released for production, because it prevents
    a "blizzard" of trace-back messages which would dismay users.

    Use plain app.run when in development mode, because the trace-back is
    generally helpful.

    Anyone have other ideas?

    Regards,
    Richard


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.719 / Virus Database: 475 - Release Date: 7/13/2004
    Richard Lionheart, Jul 15, 2004
    #2
    1. Advertising

  3. Richard Lionheart

    Ara.T.Howard Guest

    On Thu, 15 Jul 2004, Richard Lionheart wrote:

    > Hi All,
    >
    > I apologize for not testing the two approaches myself before posting the
    > question. My take on this now is:
    >
    > Use begin-rescue when code is released for production, because it prevents
    > a "blizzard" of trace-back messages which would dismay users.
    >
    > Use plain app.run when in development mode, because the trace-back is
    > generally helpful.
    >
    > Anyone have other ideas?
    >
    > Regards,
    > Richard



    require 'logger'


    logger = Logger.new STDOUT

    ....
    ....


    begin
    app.run
    rescue Exception => e
    logger.debug{ e }
    logger.fatal{ 'application failed' }
    end


    print stack trace only when debugging, give users the ability to turn
    debugging on with a command line switch.

    -a
    --
    ===============================================================================
    | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
    | PHONE :: 303.497.6469
    | A flower falls, even though we love it;
    | and a weed grows, even though we do not love it.
    | --Dogen
    ===============================================================================
    Ara.T.Howard, Jul 15, 2004
    #3
  4. Hi,

    Ara.T.Howard wrote:
    > require 'logger'


    > logger = Logger.new STDOUT


    > begin
    > app.run
    > rescue Exception => e
    > logger.debug{ e }
    > logger.fatal{ 'application failed' }
    > end
    >
    >
    > print stack trace only when debugging, give users the ability to turn
    > debugging on with a command line switch.


    And here's a sample of class Logger::Application which might be usable
    for original purpose?

    Regards,
    // NaHi

    require 'logger'

    class App < Logger::Application
    def initialize(level)
    super
    @log.level = Logger.const_get(level.upcase)
    end

    def run
    @log.debug { "debug msg" }
    @log.info { "info msg" }
    if @log.fatal? # true iif fatal msg is allowed to dump
    raise # an exception is logged as a fatal msg
    end
    0 # exit status
    end
    end

    # test

    puts "<<DEBUG"
    p App.new("DEBUG").start
    puts "DEBUG"

    puts

    puts "<<INFO"
    p App.new("INFO").start
    puts "INFO"

    puts

    puts "<<UNKNOWN"
    p App.new("UNKNOWN").start
    puts "UNKNOWN"
    NAKAMURA, Hiroshi, Jul 16, 2004
    #4
  5. Richard Lionheart

    daz Guest

    NAKAMURA, Hiroshi wrote:
    >
    > And here's a sample of class Logger::Application [...]
    >
    >[snip useful sample]



    Plenty nice !!

    Very well documented in <rubydir>/lib/logger.rb -AND/OR- ...
    <http://www.ruby-lang.org/cgi-bin/cvsweb.cgi/ruby/lib/logger.rb?rev=1.5.2.4;content-type=text%2Fplain>


    <<DEBUG
    I, [2004-07-16T04:45:30.320000 #613397] INFO -- DEBUG: Start of DEBUG.
    D, [2004-07-16T04:45:30.320000 #613397] DEBUG -- DEBUG: debug msg
    I, [2004-07-16T04:45:30.320000 #613397] INFO -- DEBUG: info msg
    F, [2004-07-16T04:45:30.320000 #613397] FATAL -- DEBUG: Detected an exception. Stopping ... (RuntimeError)
    C:/TEMP/rb1022.TMP:13:in `run'
    D:/RUBY/SRC_CVSINST/lib/ruby/1.9/logger.rb:684:in `start'
    C:/TEMP/rb1022.TMP:22
    I, [2004-07-16T04:45:30.320000 #613397] INFO -- DEBUG: End of DEBUG. (status: -1)
    -1
    DEBUG

    <<INFO
    I, [2004-07-16T04:45:30.430000 #613397] INFO -- INFO: Start of INFO.
    I, [2004-07-16T04:45:30.430000 #613397] INFO -- INFO: info msg
    F, [2004-07-16T04:45:30.430000 #613397] FATAL -- INFO: Detected an exception. Stopping ... (RuntimeError)
    C:/TEMP/rb1022.TMP:13:in `run'
    D:/RUBY/SRC_CVSINST/lib/ruby/1.9/logger.rb:684:in `start'
    C:/TEMP/rb1022.TMP:28
    I, [2004-07-16T04:45:30.430000 #613397] INFO -- INFO: End of INFO. (status: -1)
    -1
    INFO

    <<UNKNOWN
    0
    UNKNOWN


    > Regards,
    > // NaHi
    >


    :daz
    daz, Jul 16, 2004
    #5
  6. Bug? String literal %s{#{a}} cause irb to abort

    irb(main):001:0> s = "#{aaa}"
    NameError: undefined local variable or method `aaa' for main:Object
    from (irb):1
    from :0
    irb(main):002:0> s = %s{#{aaa}}
    /usr/local/lib/ruby/1.9/irb/ruby-token.rb:101:in `Token': undefined
    method `ancestors' for nil:NilClass (NoMethodError)
    from /usr/local/lib/ruby/1.9/irb/ruby-lex.rb:995:in
    `identify_string'
    from /usr/local/lib/ruby/1.9/irb/ruby-lex.rb:914:in
    `identify_quotation'
    from /usr/local/lib/ruby/1.9/irb/ruby-lex.rb:667:in `lex_int2'
    from /usr/local/lib/ruby/1.9/irb/ruby-lex.rb:664:in `call'
    from /usr/local/lib/ruby/1.9/irb/slex.rb:234:in `match_io'
    from /usr/local/lib/ruby/1.9/irb/slex.rb:219:in `match_io'
    from /usr/local/lib/ruby/1.9/irb/slex.rb:73:in `match'
    from /usr/local/lib/ruby/1.9/irb/ruby-lex.rb:277:in `token'
    ... 7 levels...
    from /usr/local/lib/ruby/1.9/irb.rb:70:in `start'
    from /usr/local/lib/ruby/1.9/irb.rb:69:in `catch'
    from /usr/local/lib/ruby/1.9/irb.rb:69:in `start'
    from /usr/local/bin/irb:13
    [alex@dhcp-89-2 alex]$ ruby -v
    ruby 1.9.0 (2004-07-08) [i686-linux]

    ------

    Meantime,

    [alex@dhcp-89-2 alex]$ ruby
    s = %s{#{aaa}}
    p s
    :"\#{aaa}"
    Alexey Verkhovsky, Jul 17, 2004
    #6
  7. Re: Bug? String literal %s{#{a}} cause irb to abort

    Hi,

    In message "Bug? String literal %s{#{a}} cause irb to abort"
    on 04/07/17, Alexey Verkhovsky <> writes:

    |irb(main):002:0> s = %s{#{aaa}}
    |/usr/local/lib/ruby/1.9/irb/ruby-token.rb:101:in `Token': undefined
    |method `ancestors' for nil:NilClass (NoMethodError)
    | from /usr/local/lib/ruby/1.9/irb/ruby-lex.rb:995:in
    |`identify_string'
    | from /usr/local/lib/ruby/1.9/irb/ruby-lex.rb:914:in
    |`identify_quotation'

    It's a bug in irb. Thank you for the report.

    matz.
    Yukihiro Matsumoto, Jul 17, 2004
    #7
  8. Richard Lionheart

    Guest

    Re: Bug? String literal %s{#{a}} cause irb to abort

    Hi,

    At Sat, 17 Jul 2004 09:05:08 +0900,
    Alexey Verkhovsky wrote in [ruby-talk:106691]:
    > irb(main):001:0> s = "#{aaa}"
    > NameError: undefined local variable or method `aaa' for main:Object
    > from (irb):1
    > from :0
    > irb(main):002:0> s = %s{#{aaa}}
    > /usr/local/lib/ruby/1.9/irb/ruby-token.rb:101:in `Token': undefined
    > method `ancestors' for nil:NilClass (NoMethodError)
    > from /usr/local/lib/ruby/1.9/irb/ruby-lex.rb:995:in
    > `identify_string'


    Does this help?


    Index: lib/irb/ruby-lex.rb
    ===================================================================
    RCS file: /cvs/ruby/src/ruby/lib/irb/ruby-lex.rb,v
    retrieving revision 1.22
    diff -U2 -p -d -r1.22 ruby-lex.rb
    --- lib/irb/ruby-lex.rb 4 Oct 2003 17:51:09 -0000 1.22
    +++ lib/irb/ruby-lex.rb 17 Jul 2004 04:35:34 -0000
    @@ -325,4 +325,5 @@ class RubyLex
    "\`" => TkDXSTRING,
    "\/" => TkDREGEXP,
    + ":" => TkDSYMBOL,
    }

    @@ -974,5 +975,5 @@ class RubyLex
    if @quoted == ch and nest == 0
    break
    - elsif @ltype != "'" && @ltype != "]" and ch == "#"
    + elsif ! "']:".index(@ltype) and ch == "#"
    subtype = true
    elsif ch == '\\' #'
    Index: lib/irb/ruby-token.rb
    ===================================================================
    RCS file: /cvs/ruby/src/ruby/lib/irb/ruby-token.rb,v
    retrieving revision 1.5
    diff -U2 -p -d -r1.5 ruby-token.rb
    --- lib/irb/ruby-token.rb 5 Aug 2003 03:08:16 -0000 1.5
    +++ lib/irb/ruby-token.rb 17 Jul 2004 04:35:26 -0000
    @@ -170,4 +170,5 @@ module RubyToken
    [:TkDXSTRING, TkNode],
    [:TkDREGEXP, TkNode],
    + [:TkDSYMBOL, TkNode],
    [:TkNTH_REF, TkNode],
    [:TkBACK_REF, TkNode],


    --
    Nobu Nakada
    , Jul 17, 2004
    #8
  9. Re: Bug? String literal %s{#{a}} cause irb to abort

    On Sat, 2004-07-17 at 08:09, wrote:

    > Does this help?
    > ... diff patch to lib/irb/ruby-lex.rb and lib/irb/ruby-token.rb ...


    Hi,

    The bug I reported earlier is not reproduced, but there is some other
    interesting behavior (three years I've spent in system test just won't
    go away :))

    [alex@dhcp-89-2 ruby]$ irb
    irb(main):001:0> a = %s{#"aaa"} // previously reported
    => :"\#\"aaa\"" // now behaves as expected

    // Another odd behavior
    irb(main):002:0> a = %s
    irb(main):003:0:
    // expected: SyntaxError
    // actual: an "empty symbol" value is assigned to a

    irb(main):004:0> a
    SyntaxError: compile error
    (irb):3: empty symbol literal
    (irb):4: syntax error
    from (irb):4
    from :0
    // expected: SyntaxError
    irb(main):009:0> a.inspect
    SyntaxError: compile error
    (irb):8: empty symbol literal
    (irb):9: syntax error
    a.inspect
    ^
    from (irb):9
    from :0
    irb(main):010:0> a.class
    => Symbol


    // empty symbol is illegal
    irb(main):011:0> a = %s{}
    SyntaxError: compile error
    (irb):11: empty symbol literal
    from (irb):11
    from :0


    Best regards,
    Alexey Verkhovsky
    Alexey Verkhovsky, Jul 17, 2004
    #9
  10. Richard Lionheart

    Guest

    Re: Bug? String literal %s{#{a}} cause irb to abort

    Hi,

    At Sat, 17 Jul 2004 22:40:34 +0900,
    Alexey Verkhovsky wrote in [ruby-talk:106711]:
    > // Another odd behavior
    > irb(main):002:0> a = %s
    > irb(main):003:0:
    > // expected: SyntaxError
    > // actual: an "empty symbol" value is assigned to a


    It is actually same as:

    $ ruby -e '%s' -e ''
    -e:2: empty symbol literal

    i.e., a symbol litral terminated by \n.

    > irb(main):004:0> a
    > SyntaxError: compile error


    But I don't know why it delays until the next line; this would
    be an irb issue.

    --
    Nobu Nakada
    , Jul 17, 2004
    #10
  11. Re: Bug? String literal %s{#{a}} cause irb to abort

    On Sat, 2004-07-17 at 17:10, wrote:

    > But I don't know why it delays until the next line;


    You are right, it doesn't assign anything to the variable, just delays
    reporting of the problem. E.g.

    [alex@dhcp-89-2 ruby]$ irb
    irb(main):001:0> a=%s
    irb(main):002:0:
    irb(main):003:0> puts "aaa"
    SyntaxError: compile error
    (irb):2: empty symbol literal
    (irb):3: syntax error
    puts "aaa"
    ^
    from (irb):3
    from :0
    Alexey Verkhovsky, Jul 17, 2004
    #11
  12. Richard Lionheart

    Mark Hubbart Guest

    Re: Bug? String literal %s{#{a}} cause irb to abort

    On Jul 17, 2004, at 7:52 AM, Alexey Verkhovsky wrote:

    > On Sat, 2004-07-17 at 17:10, wrote:
    >
    >> But I don't know why it delays until the next line;

    >
    > You are right, it doesn't assign anything to the variable, just delays
    > reporting of the problem. E.g.
    >
    > [alex@dhcp-89-2 ruby]$ irb
    > irb(main):001:0> a=%s
    > irb(main):002:0:
    > irb(main):003:0> puts "aaa"
    > SyntaxError: compile error
    > (irb):2: empty symbol literal
    > (irb):3: syntax error
    > puts "aaa"
    > ^
    > from (irb):3
    > from :0


    That's because newlines are valid delimiters in that context:

    irb(main):009:0> a=%s
    irb(main):010:0: testing
    irb(main):011:0>
    => :testing

    It delays because it's waiting for you to close out the literal.

    What you typed above is equivalent to this: a=%s{} puts "aaa"; so it
    give two errors: a compile error for the empty symbol, and a syntax
    error because to can't put puts where it is :)

    cheers,
    Mark
    Mark Hubbart, Jul 17, 2004
    #12
  13. Re: Bug? String literal %s{#{a}} cause irb to abort

    On Sat, 2004-07-17 at 20:34, Mark Hubbart wrote:
    > That's because newlines are valid delimiters in that context:


    Aha... now I understand.
    To take this hairsplitting exercise a little further, there is a slight
    difference between ruby and irb here:

    [alex@dhcp-89-2 alex]$ ruby
    %s

    -:2: empty symbol literal
    [alex@dhcp-89-2 alex]$

    [alex@dhcp-89-2 alex]$ irb
    irb(main):001:0> %s
    irb(main):002:0:
    irb(main):003:0>
    SyntaxError: compile error
    (irb):2: empty symbol literal
    from (irb):3
    from :0
    irb(main):004:0>

    So, ruby sees an error after two linefeeds, while irb does it only after
    three.

    Although, if I make a file out string "%s\n\n", both interpreters behave
    the same on it...

    I hope everybody can live with this :)

    Best regards,
    Alex
    Alexey Verkhovsky, Jul 17, 2004
    #13
  14. Richard Lionheart

    Mark Hubbart Guest

    Re: Bug? String literal %s{#{a}} cause irb to abort

    On Jul 17, 2004, at 10:53 AM, Alexey Verkhovsky wrote:

    > On Sat, 2004-07-17 at 20:34, Mark Hubbart wrote:
    >> That's because newlines are valid delimiters in that context:

    >
    > Aha... now I understand.
    > To take this hairsplitting exercise a little further,


    :)

    > there is a slight
    > difference between ruby and irb here:
    >
    > [alex@dhcp-89-2 alex]$ ruby
    > %s
    >
    > -:2: empty symbol literal
    > [alex@dhcp-89-2 alex]$
    >
    > [alex@dhcp-89-2 alex]$ irb
    > irb(main):001:0> %s
    > irb(main):002:0:
    > irb(main):003:0>
    > SyntaxError: compile error
    > (irb):2: empty symbol literal
    > from (irb):3
    > from :0
    > irb(main):004:0>
    >
    > So, ruby sees an error after two linefeeds, while irb does it only
    > after
    > three.


    In this test, I used a plus sign to indicate that the statement should
    continue to the next line:

    mark@eMac% ruby
    %s{} +
    -:1: empty symbol literal
    %s{} +
    ^

    I think irb waits on the compilation until it gets a full statement;
    while ruby compiles at the smallest expression level (but still
    line-based). So, while irb will wait to see if you try to add anything
    after that empty symbol literal, ruby grabs it right away and shows you
    where you went wrong. :)

    > I hope everybody can live with this :)


    cheers,
    Mark
    Mark Hubbart, Jul 17, 2004
    #14
  15. Re: Bug? String literal %s{#{a}} cause irb to abort

    On Sat, 2004-07-17 at 21:23, Mark Hubbart wrote:
    > I think irb waits on the compilation until it gets a full statement;
    > while ruby compiles at the smallest expression level (but still
    > line-based).


    Thanks for the explanation. Acquiring such subtle understandings is what
    makes hair-splitting exercises more than merely amusing.

    :)

    Alex
    Alexey Verkhovsky, Jul 17, 2004
    #15
  16. Thank you Ara, NaHi and Daz for the guidance and the references. My Ruby
    education continues! I'll check this out further and hopefully have no more
    questions about it.

    Best wishes,
    Richard


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.719 / Virus Database: 475 - Release Date: 7/16/2004
    Richard Lionheart, Jul 18, 2004
    #16
    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. Kumar Vijay Mishra

    PSL pros and cons

    Kumar Vijay Mishra, Sep 29, 2004, in forum: VHDL
    Replies:
    2
    Views:
    2,566
    vhdlcohen
    Oct 2, 2004
  2. Benny
    Replies:
    1
    Views:
    424
    Paul Wistrand
    Mar 1, 2004
  3. Lucas Tam
    Replies:
    3
    Views:
    1,995
    Reza Alirezaei
    Aug 9, 2005
  4. Mike Malone

    PROS/CONS: #define BEGIN {

    Mike Malone, Dec 14, 2004, in forum: C Programming
    Replies:
    60
    Views:
    1,308
    Kevin Handy
    Dec 23, 2004
  5. RichardOnRails
    Replies:
    4
    Views:
    169
    RichardOnRails
    Sep 27, 2010
Loading...

Share This Page