in reply to Who should generate a stacktrace
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.
|
|---|