Issue #8257 has been updated by headius (Charles Nutter).


rkh (Konstantin Haase) wrote:
> Should there also be a mechanism to rescue based on cause that's transparent of wrapping exceptions?

Exception::=== could be enhanced to search causes, but I'm dubious about the utility of such an enhancement. It could also make exception-handling slower; even though it's obviously *exception* handling and doesn't necessarily need to be lightning fast, people still occasionally use it for flow control inappropriately.

I'd probably punt on that one myself and see over time how common it is that people rescue based on causes. I'd suspect it's pretty rare...such a feature has never been considered for Java (as far as I know) even though Java's Throwable has had "cause" forever.

Also want to avoid feature creep. Exception#cause is a nice, simple, low-impact feature as described by me here.
----------------------------------------
Feature #8257: Exception#cause to carry originating exception along with new one
https://bugs.ruby-lang.org/issues/8257#change-38497

Author: headius (Charles Nutter)
Status: Open
Priority: Normal
Assignee: 
Category: 
Target version: 


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/


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