Movatterモバイル変換


[0]ホーム

URL:


Support for the Ruby 2.2 series has ended. Seehere for reference.

HomeClassesMethods

In Files

  • error.c

Parent

Object

Methods

Files

Class/Module Index[+]

Quicksearch
No matching classes.

Exception

Descendants of classException are used tocommunicate betweenKernel#raiseandrescue statements inbegin ... end blocks.Exception objects carry information about theexception – its type (the exception's class name), an optionaldescriptive string, and optional traceback information.Exception subclasses may add additionalinformation likeNameError#name.

Programs may make subclasses ofException,typically ofStandardError orRuntimeError, to provide custom classes andadd additional information. See the subclass list below for defaults forraise andrescue.

When an exception has been raised but not yet handled (inrescue,ensure,at_exit andEND blocks) the global variable$! will containthe current exception and$@ contains the currentexception's backtrace.

It is recommended that a library should have one subclass ofStandardError orRuntimeError and have specific exception typesinherit from it. This allows the user to rescue a generic exception typeto catch all exceptions the library may raise even if future versions ofthe library add new exception subclasses.

For example:

classMyLibraryclassError<RuntimeErrorendclassWidgetError<ErrorendclassFrobError<Errorendend

To handle both WidgetError and FrobError the library user can rescueMyLibrary::Error.

The built-in subclasses ofException are:

Public Class Methods

exception(string) → an_exception or excclick to toggle source

With no argument, or if the argument is the same as the receiver, returnthe receiver. Otherwise, create a new exception object of the same class asthe receiver, but with a message equal tostring.to_str.

new(msg = nil) → exceptionclick to toggle source

Construct a newException object, optionallypassing in a message.

                static VALUEexc_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;}

Public Instance Methods

exc == obj → true or falseclick to toggle source

Equality—Ifobj is not anException, returnsfalse. Otherwise, returnstrue ifexcandobj share same class, messages, and backtrace.

                static VALUEexc_equal(VALUE exc, VALUE obj){    VALUE mesg, backtrace;    const ID id_mesg = idMesg;    if (exc == obj) return Qtrue;    if (rb_obj_class(exc) != rb_obj_class(obj)) {        int status = 0;        ID id_message, id_backtrace;        CONST_ID(id_message, "message");        CONST_ID(id_backtrace, "backtrace");        obj = rb_protect(try_convert_to_exception, obj, &status);        if (status || obj == Qundef) {            rb_set_errinfo(Qnil);            return Qfalse;        }        if (rb_obj_class(exc) != rb_obj_class(obj)) return Qfalse;        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;}
backtrace → arrayclick to toggle source

Returns any backtrace associated with the exception. The backtrace is anarray of strings, each containing either “filename:lineNo: in`method''' or “filename:lineNo.''

defaraise"boom"enddefba()endbeginb()rescue=>detailprintdetail.backtrace.join("\n")end

produces:

prog.rb:2:in `a'prog.rb:6:in `b'prog.rb:10
                static VALUEexc_backtrace(VALUE exc){    ID bt;    VALUE obj;    CONST_ID(bt, "bt");    obj = rb_attr_get(exc, bt);    if (rb_backtrace_p(obj)) {        obj = rb_backtrace_to_str_ary(obj);        /* rb_iv_set(exc, "bt", obj); */    }    return obj;}
backtrace_locations → arrayclick to toggle source

Returns any backtrace associated with the exception. This method is similarto#backtrace, but thebacktrace is an array of

Thread::Backtrace::Location.

Now, this method is not affected by#set_backtrace.

                static VALUEexc_backtrace_locations(VALUE exc){    ID bt_locations;    VALUE obj;    CONST_ID(bt_locations, "bt_locations");    obj = rb_attr_get(exc, bt_locations);    if (!NIL_P(obj)) {        obj = rb_backtrace_to_location_ary(obj);    }    return obj;}
cause → an_exception or nilclick to toggle source

Returns the previous exception ($!) at the time this exception was raised.This is useful for wrapping exceptions and retaining the original exceptioninformation.

                VALUEexc_cause(VALUE exc){    ID id_cause;    CONST_ID(id_cause, "cause");    return rb_attr_get(exc, id_cause);}
exception(string) → an_exception or excclick to toggle source

With no argument, or if the argument is the same as the receiver, returnthe receiver. Otherwise, create a new exception object of the same class asthe receiver, but with a message equal tostring.to_str.

                static VALUEexc_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;}
inspect → stringclick to toggle source

Return this exception's class name and message

                static VALUEexc_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;}
message → stringclick to toggle source

Returns the result of invokingexception.to_s. Normally thisreturns the exception's message or name. By supplying a to_str method,exceptions are agreeing to be used where Strings are expected.

                static VALUEexc_message(VALUE exc){    return rb_funcall(exc, rb_intern("to_s"), 0, 0);}
set_backtrace(backtrace) → arrayclick to toggle source

Sets the backtrace information associated withexc. Thebacktrace must be an array ofStringobjects or a singleString in the formatdescribed in#backtrace.

                static VALUEexc_set_backtrace(VALUE exc, VALUE bt){    return rb_iv_set(exc, "bt", rb_check_backtrace(bt));}
to_s → stringclick to toggle source

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

                static VALUEexc_to_s(VALUE exc){    VALUE mesg = rb_attr_get(exc, rb_intern("mesg"));    if (NIL_P(mesg)) return rb_class_name(CLASS_OF(exc));    return rb_String(mesg);}

This page was generated for Ruby 2.2.0

Generated with Ruby-doc Rdoc Generator 0.44.0.


[8]ページ先頭

©2009-2025 Movatter.jp