When raising a new exception in a rescue
block, Ruby 2.1+ automatically remembers the original exception as Exception#cause
.
Nested exceptions for Ruby:
When you rescue an error and then re-raise your own, you don't have to lose track of what actually occured, you can keep/nest the old error in your own and the stacktrace will reflect the cause of the original error.
This is awesome when you classes convert exception classes. I now always subclass Nesty::NestedStandardError
instead of StandardError
for my own error classes.
About Exception#cause
Ruby 2.1 has a built-in mechanism with Exception#cause
, which serves a similiar purpose as Nesty. However, any code printing your stack trace (Exception Notifier, Airbrake, Sentry, your IRB shell, etc.) needs to be aware of Exception#cause
. Nesty on the other hand-merges the stack trace array of the current and previous exception.
This way Nesty does not need to rely on other tools to display Exception#cause
.
There is also a backport of Exception#cause for older Rubies Show archive.org snapshot .