[ruby-core:84377] [CommonRuby Feature#8257] Exception#cause to carry originating exception along with new one

From: eregontp@...
Date: 2017-12-20 17:35:33 UTC
List: ruby-core #84377
Issue #8257 has been updated by Eregon (Benoit Daloze).


ko1 (Koichi Sasada) wrote:
>  What happen on exception from deep backtrace, occurred by other more
>  deeper exception?  Show all long two backtraces?

Yes, although we can omit the common part of the backtrace in the cause backtraces like in Java:
http://www.codejava.net/java-core/exception/understanding-exception-stack-trace-in-java-with-code-examples

peterfaiman (Peter Faiman) wrote:
> Is there a reason not to do caused-by stack trace printing? Or has it just not been implemented by anyone yet?

I believe it was just no implemented yet.
@ko1 Who should we assign this to?

----------------------------------------
Feature #8257: Exception#cause to carry originating exception along with new one
https://bugs.ruby-lang.org/issues/8257#change-68566

* Author: headius (Charles Nutter)
* Status: Closed
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: Ruby 2.1.0
----------------------------------------
Often when a lower-level API raises an exception, we would like to re-raise a different exception specific to our API or library. Currently in Ruby, only our new exception is ever seen by users; the original exception is lost forever, unless the user decides to dig around our library and log it. We need a way to have an exception carry a "cause" along with it.

Java has getCause/setCause and standard constructors that take a cause exception. Printing out an exception's backtrace then reports both that exception and any "cause" exception.

Rubinius has added a similar feature: https://gist.github.com/dbussink/b2e01e51d0c50b27004f

The changes required for this feature are pretty benign:

* Exception#cause and #cause= accessors.
* A new set of Kernel#raise overloads that accept (as a trailing argument, probably) the "cause" exception.
* Modifications to backtrace-printing logic to also display backtrace information from the "cause" exception (and in turn, from any nested "cause" exceptions).

There's some discussion here about alternatives to #cause, none of which are quite as elegant as having it built in: http://www.skorks.com/2013/04/ruby-why-u-no-have-nested-exceptions/



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

In This Thread

Prev Next