Skip to content
class method

Exception.exception

(?::string | ::_ToS msg) -> instance
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:

Example
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:

Example
x1 = x0.exception('Boom') # => #<StandardError: Boom>
x0..equal?(x1)            # => false
See also #exception .json_create .new also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0