Descendants of class Exceptionare used to communicate between raisemethods and rescuestatements in begin/endblocks. Exceptionobjects carry information about the exception—its type (the exception's class name), an optional descriptive string, and optional traceback information. Programs may subclass Exception, or more typically StandardErrorto provide custom classes and add additional information.

Exception serialization/deserialization

Methods
#
A
B
E
I
J
M
N
S
T
Class Public methods
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.

json_create(object)

Deserializes JSON string by constructing new Exception object with message mand backtrace bserialized with to_json

# File ext/json/lib/json/add/core.rb, line 198
def self.json_create(object)
  result = new(object['m'])
  result.set_backtrace object['b']
  result
end
Exception.new(msg = nil) → exception

Construct a new Exception object, optionally passing in a message.

static VALUE
exc_initialize(int argc, VALUE *argv, VALUE exc)
{
    VALUE arg;

    rb_scan_args(argc, argv, "01", &arg);
    rb_iv_set(exc, "mesg", arg);
    rb_iv_set(exc, "bt", Qnil);

    return exc;
}
Instance Public methods
exc == obj → true or false

Equality—If objis not an Exception, returns false. Otherwise, returns trueif exc and objshare same class, messages, and backtrace.

static VALUE
exc_equal(VALUE exc, VALUE obj)
{
    VALUE mesg, backtrace;
    ID id_mesg;

    if (exc == obj) return Qtrue;
    CONST_ID(id_mesg, "mesg");

    if (rb_obj_class(exc) != rb_obj_class(obj)) {
        ID id_message, id_backtrace;
        CONST_ID(id_message, "message");
        CONST_ID(id_backtrace, "backtrace");

        mesg = rb_check_funcall(obj, id_message, 0, 0);
        if (mesg == Qundef) return Qfalse;
        backtrace = rb_check_funcall(obj, id_backtrace, 0, 0);
        if (backtrace == Qundef) return Qfalse;
    }
    else {
        mesg = rb_attr_get(obj, id_mesg);
        backtrace = exc_backtrace(obj);
    }

    if (!rb_equal(rb_attr_get(exc, id_mesg), mesg))
        return Qfalse;
    if (!rb_equal(exc_backtrace(exc), backtrace))
        return Qfalse;
    return Qtrue;
}
as_json(*)

Returns a hash, that will be turned into a JSON object and represent this object.

# File ext/json/lib/json/add/core.rb, line 206
def as_json(*)
  {
    JSON.create_id => self.class.name,
    'm'            => message,
    'b'            => backtrace,
  }
end
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
static VALUE
exc_backtrace(VALUE exc)
{
    ID bt;

    CONST_ID(bt, "bt");
    return rb_attr_get(exc, bt);
}
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.

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

    if (argc == 0) return self;
    if (argc == 1 && self == argv[0]) return self;
    exc = rb_obj_clone(self);
    exc_initialize(argc, argv, exc);

    return exc;
}
exception.inspect → string

Return this exception's class name an message

static VALUE
exc_inspect(VALUE exc)
{
    VALUE str, klass;

    klass = CLASS_OF(exc);
    exc = rb_obj_as_string(exc);
    if (RSTRING_LEN(exc) == 0) {
        return rb_str_dup(rb_class_name(klass));
    }

    str = rb_str_buf_new2("#<");
    klass = rb_class_name(klass);
    rb_str_buf_append(str, klass);
    rb_str_buf_cat(str, ": ", 2);
    rb_str_buf_append(str, exc);
    rb_str_buf_cat(str, ">", 1);

    return str;
}
exception.message → 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.

static VALUE
exc_message(VALUE exc)
{
    return rb_funcall(exc, rb_intern("to_s"), 0, 0);
}
exc.set_backtrace(array) → array

Sets the backtrace information associated with exc. The argument must be an array of Stringobjects in the format described in Exception#backtrace.

static VALUE
exc_set_backtrace(VALUE exc, VALUE bt)
{
    return rb_iv_set(exc, "bt", rb_check_backtrace(bt));
}
to_json(*args)

Stores class name (Exception) with message mand backtrace array bas JSON string

# File ext/json/lib/json/add/core.rb, line 216
def to_json(*args)
  as_json.to_json(*args)
end
exception.to_s → string

Returns exception's message (or the name of the exception if no message is set).

static VALUE
exc_to_s(VALUE exc)
{
    VALUE mesg = rb_attr_get(exc, rb_intern("mesg"));
    VALUE r = Qnil;

    if (NIL_P(mesg)) return rb_class_name(CLASS_OF(exc));
    r = rb_String(mesg);
    return r;
}