How to print FULL stacktrace of exception w/ line #?

Discussion in 'Ruby' started by Andrew Chen, Sep 19, 2007.

  1. Andrew Chen

    Andrew Chen Guest

    The ruby interpreter prints out a full trace of the exception.

    However, all the expection I got is an one line message. I need to know
    where the exception happened in my code. I don't mind it is verbose.

    begin
    ....
    Math.sqrt (-1)
    rescue => e
    puts e.inspect
    end

    e only prints out a message.

    Am I missing anything real simple?

    Thanks
    ~Andrew Chen
    --
    Posted via http://www.ruby-forum.com/.
     
    Andrew Chen, Sep 19, 2007
    #1
    1. Advertising

  2. Andrew Chen

    Konrad Meyer Guest

    --nextPart6517373.X3G5RxmhWM
    Content-Type: text/plain;
    charset="utf-8"
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    Quoth Andrew Chen:
    > The ruby interpreter prints out a full trace of the exception.
    >=20
    > However, all the expection I got is an one line message. I need to know
    > where the exception happened in my code. I don't mind it is verbose.
    >=20
    > begin
    > ....
    > Math.sqrt (-1)
    > rescue =3D> e
    > puts e.inspect
    > end
    >=20
    > e only prints out a message.
    >=20
    > Am I missing anything real simple?
    >=20
    > Thanks
    > ~Andrew Chen


    Don't rescue it, full stack trace! :p

    =2D-=20
    Konrad Meyer <> http://konrad.sobertillnoon.com/

    --nextPart6517373.X3G5RxmhWM
    Content-Type: application/pgp-signature; name=signature.asc
    Content-Description: This is a digitally signed message part.

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.7 (GNU/Linux)

    iD8DBQBG8JHUCHB0oCiR2cwRApPcAJ4gnmPf6H9PWmf8ezFZbqGLrGEj9gCgnWl7
    HqOiM6r6rZ153Ds4SU5CFag=
    =NHce
    -----END PGP SIGNATURE-----

    --nextPart6517373.X3G5RxmhWM--
     
    Konrad Meyer, Sep 19, 2007
    #2
    1. Advertising

  3. Andrew Chen

    Andrew Chen Guest

    Konrad Meyer wrote:
    >
    > Don't rescue it, full stack trace! :p


    In the situation, I want to rescue, and run some other logic, since I
    don't want the whole class to die.
    --
    Posted via http://www.ruby-forum.com/.
     
    Andrew Chen, Sep 19, 2007
    #3
  4. On 9/19/07, Andrew Chen <> wrote:
    > The ruby interpreter prints out a full trace of the exception.
    >
    > However, all the expection I got is an one line message. I need to know
    > where the exception happened in my code. I don't mind it is verbose.


    begin
    ....
    Math.sqrt (-1)
    rescue => e
    puts e.inspect
    puts e.backtrace
    ensure
    andrew.brain << open("ri"){|ri| ri.read('Exception')}
    end
     
    Michael Fellinger, Sep 19, 2007
    #4
  5. Sylvain Joyeux, Sep 19, 2007
    #5

  6. > Don't rescue it, full stack trace! :p

    ... or not
    Ruby removes in-betweeen lines if a backtrace is 'too long'. The limit is
    not configurable, so it you have long backtraces (and if you need what is
    in the middle) you're fucked.

    --
    Sylvain Joyeux
     
    Sylvain Joyeux, Sep 19, 2007
    #6
  7. Andrew Chen

    Mitch Mon Guest

    I miss Java.

    I run into this kind of short sighted decision far too often with Ruby.
    Ugh.
    --
    Posted via http://www.ruby-forum.com/.
     
    Mitch Mon, Sep 25, 2007
    #7
  8. Andrew Chen

    Alex Young Guest

    Sylvain Joyeux wrote:
    >> Don't rescue it, full stack trace! :p

    > ... or not
    > Ruby removes in-betweeen lines if a backtrace is 'too long'. The limit is
    > not configurable, so it you have long backtraces (and if you need what is
    > in the middle) you're fucked.
    >

    You can get that information when you rescue the exception -
    Exception#backtrace is an array of caller information.

    --
    Alex
     
    Alex Young, Sep 26, 2007
    #8
  9. > You can get that information when you rescue the exception -
    > Exception#backtrace is an array of caller information.

    Yes, so I had to rescue all exceptions in all threads I spawn to have a
    proper backtrace (even more so since with abort_on_exception the exception
    is not shown /at all/).

    Sometimes it drived me crazy ...

    Sylvain Joyeux
     
    Sylvain Joyeux, Sep 26, 2007
    #9
  10. Andrew Chen

    Roger Pack Guest

    Sylvain Joyeux wrote:
    >> You can get that information when you rescue the exception -
    >> Exception#backtrace is an array of caller information.

    > Yes, so I had to rescue all exceptions in all threads I spawn to have a
    > proper backtrace (even more so since with abort_on_exception the
    > exception
    > is not shown /at all/).
    >
    > Sometimes it drived me crazy ...
    >
    > Sylvain Joyeux


    You might be able to rewrite the Thread.new method and have it catch and
    display exceptions (and raise them).
    GL!
    -Roger
    --
    Posted via http://www.ruby-forum.com/.
     
    Roger Pack, Sep 26, 2007
    #10
  11. Andrew Chen

    Andrew Chen Guest

    e.backtrace works fine for me. In my case, I don't need traces in the
    middle.

    Thanks for all the help!

    --
    Posted via http://www.ruby-forum.com/.
     
    Andrew Chen, Sep 26, 2007
    #11
  12. Andrew Chen

    Kru K. Guest

    Michael Fellinger wrote in post #555437:
    > On 9/19/07, Andrew Chen <> wrote:
    >> The ruby interpreter prints out a full trace of the exception.
    >>
    >> However, all the expection I got is an one line message. I need to know
    >> where the exception happened in my code. I don't mind it is verbose.

    >
    > begin
    > ....
    > Math.sqrt (-1)
    > rescue => e
    > puts e.inspect
    > puts e.backtrace
    > ensure
    > andrew.brain << open("ri"){|ri| ri.read('Exception')}
    > end


    hahah :) nice one

    --
    Posted via http://www.ruby-forum.com/.
     
    Kru K., Oct 14, 2010
    #12
    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. S. Justin Gengo
    Replies:
    0
    Views:
    729
    S. Justin Gengo
    Aug 5, 2003
  2. Sebastien
    Replies:
    3
    Views:
    8,632
    chinnaswamyg
    Sep 28, 2006
  3. chinnaswamyg
    Replies:
    0
    Views:
    667
    chinnaswamyg
    Sep 28, 2006
  4. Elaine Cheong
    Replies:
    1
    Views:
    1,015
    Gordon Beaton
    Dec 9, 2006
  5. Samuel
    Replies:
    3
    Views:
    271
    Samuel
    Jun 22, 2007
Loading...

Share This Page