Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Strack traces with Exception::Class

by Arunbear (Prior)
on Nov 03, 2003 at 17:13 UTC ( [id://304164]=perlquestion: print w/replies, xml ) Need Help??

Arunbear has asked for the wisdom of the Perl Monks concerning the following question:

Hello friends,

is there a way to configure Exception::Class so that Perl will show the full stracktrace without myself having to first catch the exception and then call the trace method?

i.e. I want $my_execpt->throw('error_msg') to have the same effect as confess('error_msg')

I need this behaviour because there are times when I want to raise an exception (e.g. subroutine called without a required parameter) but don't want to write a handler for it - and I still need the stracktrace.


Thanks for any assistance.

Replies are listed 'Best First'.
Re: Strack traces with Exception::Class
by hardburn (Abbot) on Nov 03, 2003 at 17:24 UTC

    I think it will work if use the Carp module and import verbose, e.g.:

    use Carp 'verbose';

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

      I've now tried using Carp with the verbose option, but it does not make Exception::Class's throw method give the strack trace.
Re: Strack traces with Exception::Class
by IlyaM (Parson) on Nov 04, 2003 at 10:24 UTC

    From Exception::Class documentation:

    * Trace($boolean) Each "Exception::Class::Base" subclass can be set individually to include a a stracktrace when the "as_string" method is called. The default is to not include a stacktrace. Calling this method with a value changes this behavior. It always returns the current value (after any change is applied). This value is inherited by any subclasses. However, if this value is set for a subclass, it will there- after be independent of the value in "Excep- tion::Class::Base". This is a class method, not an object method.

    I.e if you need stacktraces for exception class MyException (and its childs) you need to add

    MyException->Trace(1);

    Alternatively you can just redefine as_string() method to print uncaught exceptions as you wish.

    --
    Ilya Martynov, ilya@iponweb.net
    CTO IPonWEB (UK) Ltd
    Quality Perl Programming and Unix Support UK managed @ offshore prices - http://www.iponweb.net
    Personal website - http://martynov.org

      Hopefully, this isn't too old to post to.

      I just had to solve this problem myself. I wanted all exceptions to show stack traces all the time. Since Trace($boolean) is a class method, I just did this (see below) once, and it worked for all child/sub classes and their objects.

      Exception::Class::Base->Trace(1);

      I put that right after my use statement.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://304164]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 12:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found