instance method
Exception#cause
() -> ::Exception?
cause → exception or nil
error.c
View on GitHub ↗
static VALUE
exc_cause(VALUE exc)
{
return rb_attr_get(exc, id_cause);
}
Returns the previous value of global variable $!, which may be nil (see Global Variables):
Example
begin raise('Boom 0') rescue => x0 puts "Exception: #{x0}; $!: #{$!}; cause: #{x0.cause.inspect}." begin raise('Boom 1') rescue => x1 puts "Exception: #{x1}; $!: #{$!}; cause: #{x1.cause}." begin raise('Boom 2') rescue => x2 puts "Exception: #{x2}; $!: #{$!}; cause: #{x2.cause}." end end end
Output:
Exception: Boom 0; $!: Boom 0; cause: nil. Exception: Boom 1; $!: Boom 1; cause: Boom 0. Exception: Boom 2; $!: Boom 2; cause: Boom 1.
See also
#backtrace_locations
#detailed_message
#backtrace
#exception
also in
4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0