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
-
.exception ⇒ Object
call-seq: exc.exception(string) -> an_exception or exc.
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.
347 348 349 |
# File 'error.c', line 347 static VALUE exc_initialize(argc, argv, exc) int argc; |
Class Method Details
.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
.
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
486 487 488 |
# File 'error.c', line 486 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
.
375 376 377 |
# File 'error.c', line 375 static VALUE exc_exception(argc, argv, self) int argc; |
#inspect ⇒ String
Return this exception’s class name an message
435 436 437 |
# File 'error.c', line 435 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.
421 422 423 |
# File 'error.c', line 421 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
.
529 530 531 |
# File 'error.c', line 529 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).
399 400 401 |
# File 'error.c', line 399 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.
421 422 423 |
# File 'error.c', line 421 static VALUE exc_to_str(exc) VALUE exc; |