Method: Exception#exception

Defined in:
error.c

#exception(*args) ⇒ Object

call-seq:

exception(message = nil) -> self or new_exception

Returns an exception object of the same class as self; useful for creating a similar exception, but with a different message.

With message nil, returns self:

x0 = StandardError.new('Boom') # => #<StandardError: Boom>
x1 = x0.exception              # => #<StandardError: Boom>
x0.__id__ == x1.__id__         # => true

With string-convertible object message (even the same as the original message), returns a new exception object whose class is the same as self, and whose message is the given message:

x1 = x0.exception('Boom') # => #<StandardError: Boom>
x0..equal?(x1)            # => false


1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
# File 'error.c', line 1551

static VALUE
exc_exception(int argc, VALUE *argv, VALUE self)
{
    VALUE exc;

    argc = rb_check_arity(argc, 0, 1);
    if (argc == 0) return self;
    if (argc == 1 && self == argv[0]) return self;
    exc = rb_obj_clone(self);
    rb_ivar_set(exc, id_mesg, argv[0]);
    return exc;
}