Ruby/Справочник/Exception
Класс Exception
[править]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.
Add file-blaming to exceptions
Методы класса
Методы объекта
backtrace, exception, inspect, message, set_backtrace, to_str, to_s, to_yaml
Exception::exception
[править]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.
Exception::new
[править]Exception.new(msg = nil) => exception
Construct a new Exception object, optionally passing in a message.
Exception#backtrace
[править]exception.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
Exception#exception
[править]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.
Exception#inspect
[править]exception.inspect => string
Return this exception's class name an message
Exception#message
[править]exception.message => string exception.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.
Exception#set_backtrace
[править]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.
Exception#to_s
[править]exception.to_s => string
Returns exception's message (or the name of the exception if no message is set).
Exception#to_str
[править]exception.message => string exception.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.