Exception: Exception
Overview
Descendents of class Exception
are used to communicate between raise
methods and rescue
statements in begin/end
blocks. Exception
objects carry information about the exception—its type (the exception’s class name), an optional descriptive string, and optional traceback information. Programs may subclass Exception
to add additional information.
Direct Known Subclasses
NoMemoryError, ScriptError, SignalException, StandardError, SystemExit, fatal
Class Method Summary collapse
-
.new(args, ...) ⇒ Object
Calls
allocate
to create a new object of class’s class, then invokes that object’sinitialize
method, passing it args.
Instance Method Summary collapse
-
#backtrace ⇒ Array
Returns any backtrace associated with the exception.
-
#exception ⇒ Object
call-seq: exc.exception(string) -> an_exception or exc.
-
#new(msg = nil) ⇒ Exception
constructor
Construct a new Exception object, optionally passing in a message.
-
#inspect ⇒ String
Return this exception’s class name an message.
-
#message ⇒ Object
Returns the result of invoking
exception.to_s
. -
#set_backtrace(array) ⇒ Array
Sets the backtrace information associated with exc.
-
#to_s ⇒ String
Returns exception’s message (or the name of the exception if no message is set).
-
#to_str ⇒ Object
Returns the result of invoking
exception.to_s
.
Constructor Details
#new(msg = nil) ⇒ Exception
Construct a new Exception object, optionally passing in
a message.
348 349 350 |
# File 'error.c', line 348 static VALUE exc_initialize(argc, argv, exc) int argc; |
Class Method Details
.new(args, ...) ⇒ Object
Calls allocate
to create a new object of class’s class, then invokes that object’s initialize
method, passing it args. This is the method that ends up getting called whenever an object is constructed using .new.
1585 1586 1587 |
# File 'object.c', line 1585 VALUE rb_class_new_instance(argc, argv, klass) int argc; |
Instance Method Details
#backtrace ⇒ Array
Returns any backtrace associated with the exception. The backtrace is an array of strings, each containing either “filename:lineNo: in ‘method”’ or “filename:lineNo.”
def a
raise "boom"
end
def b
a()
end
begin
b()
rescue => detail
print detail.backtrace.join("\n")
end
produces:
prog.rb:2:in `a'
prog.rb:6:in `b'
prog.rb:10
487 488 489 |
# File 'error.c', line 487 static VALUE exc_backtrace(exc) VALUE exc; |
#exception ⇒ Object
call-seq:
exc.exception(string) -> an_exception or exc
With no argument, or if the argument is the same as the receiver, return the receiver. Otherwise, create a new exception object of the same class as the receiver, but with a message equal to string.to_str
.
376 377 378 |
# File 'error.c', line 376 static VALUE exc_exception(argc, argv, self) int argc; |
#inspect ⇒ String
Return this exception’s class name an message
436 437 438 |
# File 'error.c', line 436 static VALUE exc_inspect(exc) VALUE exc; |
#message ⇒ String #to_str ⇒ String
Returns the result of invoking exception.to_s
. Normally this returns the exception’s message or name. By supplying a to_str method, exceptions are agreeing to be used where Strings are expected.
422 423 424 |
# File 'error.c', line 422 static VALUE exc_to_str(exc) VALUE exc; |
#set_backtrace(array) ⇒ Array
Sets the backtrace information associated with exc. The argument must be an array of String
objects in the format described in Exception#backtrace
.
530 531 532 |
# File 'error.c', line 530 static VALUE exc_set_backtrace(exc, bt) VALUE exc; |
#to_s ⇒ String
Returns exception’s message (or the name of the exception if no message is set).
400 401 402 |
# File 'error.c', line 400 static VALUE exc_to_s(exc) VALUE exc; |
#message ⇒ String #to_str ⇒ String
Returns the result of invoking exception.to_s
. Normally this returns the exception’s message or name. By supplying a to_str method, exceptions are agreeing to be used where Strings are expected.
422 423 424 |
# File 'error.c', line 422 static VALUE exc_to_str(exc) VALUE exc; |