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

Dear monks,

I've got another question regarding the same topic of "handling errors" and your help would be really appreciated. This time I'm thinking about the best way of passing the stacktrace from the deepest bottom to the top caller.

I'd better explain my question by some example. Let's assume, we have some some classes and some methods:

package ClassB; sub method_y { if($something_has_gone_wrong) { confess("Something has gone wrong!"); } } package ClassA; sub method_x { try { $class_b_object->method_y; } catch { confess($_); } } package main; try { $class_a_object->method_x } catch { $log->error($_); }

I need to have a stack-trace to be delivered to the main module, but, obviously, when something goes wrong, I have 2 stack-traces instead of 1 (one is from ClassA::method_x and another - from ClassB::method_y), which is really ugly. I need only the stack-trace from the bottom, but I don't want to decide whenever I shall die() or confess() in every catch-block.

As a solution, I imagine something like that:

package Error; sub new { my($self, $class, $parameter) if(ref($parameter) eq 'Error') { return($parameter); } else { bless($self, $class); $self->stacktrace(Devel::StackTrace->new->as_string); $self->message($parameter); $self->timestamp(time); $self->whatever("Blah-blah"); return($self); } } package ClassB; sub method_y { if($something_has_gone_wrong) { die(Error->new("Something has gone wrong")); } } package ClassA; sub method_x { try { $class_b_object->method_y } catch { die(Error->new($_)); } } package main; try { $class_a_object->method_x; } catch { if(ref($_) eq Error) { print(SOMEWHERE "The worst has happened at " . $_->timestamp . "\n +"); print(SOMEWHERE $_->message . "\n"); print(SOMEWHERE $_->backtrace . "\n"); } else { $log->error($_); } }

So, the deepest subroutine (ClassB::method_y) dies and creates a new Error-object, which has a stacktrace and everything else we need. This object is being passed as $_ to the constructor of another Error-object inside of ClassA::method_x, but it won't add anything to this object, because it will understand that it have got an Error-object, not just an error-message. Then the main module will do something with that object.

I'm not sure about this idea, so I'll be glad to know your opinion on that.

Thank you very much!

V.Melnik

Replies are listed 'Best First'.
Re: Who should generate a stacktrace
by Anonymous Monk on Oct 03, 2014 at 20:50 UTC

    Maybe you've oversimplified something, because I don't understand why ClassA::method_x catches and just re-throws an error when it could just not catch it in the first place?

    As for the concept, I think one needs to differentiate between a pure catch-and-rethrow of an exception, and a wrapping of an exception inside another exception. Also think about debugging: Does someone inspecting the stack traces need to know that the exception was handled in ClassA::method_x? If so, then it's necessary to preserve the original stack trace, and perhaps you should look into how to make the displaying of multiple stack traces more user-friendly instead of hiding the second stack trace!

    Just one example is Java (source):

    Exception in thread "main" java.lang.IllegalStateException: A book has + a null property at com.example.myproject.Author.getBookIds(Author.java:38) at com.example.myproject.Bootstrap.main(Bootstrap.java:14) Caused by: java.lang.NullPointerException at com.example.myproject.Book.getId(Book.java:22) at com.example.myproject.Author.getBookIds(Author.java:35) ... 1 more

    I don't find that "ugly" - personally, if I'm really deep into debugging something, I'd like all the information.

    Other than that, your pseudo-code is ok. I might name the method new_or_rethrow instead of new to avoid confusion about what it does. Also, don't use ref to determine if something is an object, use something like UNIVERSAL's isa.

    However, it seems like you're moving into reinventing-the-wheel territory. Have a look at CPAN! Just one example is Exception::Class.