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.


In reply to Re: Who should generate a stacktrace by Anonymous Monk
in thread Who should generate a stacktrace by v_melnik

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.