[Q] Catching exceptions at top level

Discussion in 'Ruby' started by Laurent Julliard, Nov 25, 2004.

  1. I have sort of a trivial question here that I'm afraid is so simple
    that I could not find the answer :-(

    I'd like to be able to catch all unrescued exceptions raised in a Ruby
    script in the END block that is executed last before the script
    terminates. The goal is to intercept the the exception raised and
    process the exception message and stack trace before it is printed

    Shall I redefine the raise method to do this or is there another way?

    Thanks for your help!

    Laurent
     
    Laurent Julliard, Nov 25, 2004
    #1
    1. Advertising

  2. Laurent Julliard wrote:

    > I have sort of a trivial question here that I'm afraid is so simple that
    > I could not find the answer :-(
    >
    > I'd like to be able to catch all unrescued exceptions raised in a Ruby
    > script in the END block that is executed last before the script
    > terminates. The goal is to intercept the the exception raised and
    > process the exception message and stack trace before it is printed


    I think you can just edit the object that is in $!. If you want a
    solution without using perlish variables wrap the whole application in a
    rescue clause like this:

    begin
    code
    more code
    and so on ...
    rescue Exception => error
    do something with error
    raise # reraise the exception
    end
     
    Florian Gross, Nov 25, 2004
    #2
    1. Advertising

  3. Florian Gross wrote:
    > Laurent Julliard wrote:
    >
    >> I have sort of a trivial question here that I'm afraid is so simple
    >> that I could not find the answer :-(
    >>
    >> I'd like to be able to catch all unrescued exceptions raised in a Ruby
    >> script in the END block that is executed last before the script
    >> terminates. The goal is to intercept the the exception raised and
    >> process the exception message and stack trace before it is printed

    >
    >
    > I think you can just edit the object that is in $!. If you want a
    > solution without using perlish variables wrap the whole application in a
    > rescue clause like this:
    >
    > begin
    > code
    > more code
    > and so on ...
    > rescue Exception => error
    > do something with error
    > raise # reraise the exception
    > end
    >
    >


    The problem is that I must leave the original file containing the code
    unchanged so it has to be a mechanism that i can include with a piece
    of code added at runtime with a -r option

    Laurent

    --
    Laurent JULLIARD
    http://www.moldus.org/~laurent
     
    Laurent Julliard, Nov 25, 2004
    #3
  4. In Message-Id: <>
    Laurent Julliard <> writes:

    > The problem is that I must leave the original file containing the code
    > unchanged so it has to be a mechanism that i can include with a piece
    > of code added at runtime with a -r option


    How about the following?

    original.rb:
    # your original application code.

    wrapper.rb:
    begin
    load("original.rb")
    rescue Exception => e
    # transform and reraise.
    end

    then invoking wrapper.rb.


    --
    November 26, 2004
    Slow and steady wins the race.
     
    YANAGAWA Kazuhisa, Nov 25, 2004
    #4
  5. Laurent Julliard <> wrote in news:41A654AB.1070002
    @moldus.org:

    > The problem is that I must leave the original file containing the code
    > unchanged so it has to be a mechanism that i can include with a piece
    > of code added at runtime with a -r option


    The solution was there, but I will make that explicit:

    catcher.rb ---------------------------------------
    END {
    puts 'exception has happened.'
    p $!
    }
    -----------------------------------------------------

    test.rb ---------------------------------------------
    raise 'dodah'
    -----------------------------------------------------

    Call as
    ruby -rcatcher test.rb

    gives you
    exception has happened.
    #<RuntimeError: dodah>
    test.rb:1: dodah (RuntimeError)


    yours, kaspar

    hand manufactured code - www.tua.ch/ruby
     
    Kaspar Schiess, Nov 26, 2004
    #5
  6. Kaspar Schiess wrote:
    > Laurent Julliard <> wrote in news:41A654AB.1070002
    > @moldus.org:
    >
    >
    >>The problem is that I must leave the original file containing the code
    >>unchanged so it has to be a mechanism that i can include with a piece
    >>of code added at runtime with a -r option

    >
    >
    > The solution was there, but I will make that explicit:
    >
    > catcher.rb ---------------------------------------
    > END {
    > puts 'exception has happened.'
    > p $!
    > }
    > -----------------------------------------------------
    >
    > test.rb ---------------------------------------------
    > raise 'dodah'
    > -----------------------------------------------------
    >
    > Call as
    > ruby -rcatcher test.rb
    >
    > gives you
    > exception has happened.
    > #<RuntimeError: dodah>
    > test.rb:1: dodah (RuntimeError)
    >
    >
    > yours, kaspar
    >
    > hand manufactured code - www.tua.ch/ruby
    >
    >
    >


    This is exactly what I was lloking for and as I suspected when I first
    asked the question the answer wass almost obvious :) Thanks again

    Laurent

    --
    Laurent JULLIARD
    http://www.moldus.org/~laurent
     
    Laurent Julliard, Nov 27, 2004
    #6
    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. Marina
    Replies:
    2
    Views:
    478
    Marina
    Jul 8, 2003
  2. Amil Hanish
    Replies:
    0
    Views:
    552
    Amil Hanish
    Apr 13, 2006
  3. Adam Maass
    Replies:
    5
    Views:
    408
    Sudsy
    Jul 22, 2003
  4. Mike Schilling
    Replies:
    2
    Views:
    353
    Mike Schilling
    Jul 16, 2003
  5. pabbu
    Replies:
    8
    Views:
    733
    Marc Boyer
    Nov 7, 2005
Loading...

Share This Page