首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

SystemCallError

Parent:StandardError

SystemCallError是所有低级平台相关错误的基类。

当前平台上可用的错误是SystemCallError的子类,并在Errno模块中定义。

代码语言:javascript
复制
File.open("does/not/exist")

引发了一个例外:

代码语言:javascript
复制
Errno::ENOENT: No such file or directory - does/not/exist

公共类方法

system_call_error === other → true or false Show source

如果接收者是一个通用的SystemCallError,或者错误号self和other是相同的,则返回true。

代码语言:javascript
复制
static VALUE
syserr_eqq(VALUE self, VALUE exc)
{
    VALUE num, e;

    if (!rb_obj_is_kind_of(exc, rb_eSystemCallError)) {
        if (!rb_respond_to(exc, id_errno)) return Qfalse;
    }
    else if (self == rb_eSystemCallError) return Qtrue;

    num = rb_attr_get(exc, id_errno);
    if (NIL_P(num)) {
        num = rb_funcallv(exc, id_errno, 0, 0);
    }
    e = rb_const_get(self, id_Errno);
    if (FIXNUM_P(num) ? num == e : rb_equal(num, e))
        return Qtrue;
    return Qfalse;
}

new(msg, errno) → system_call_error_subclass Show source

如果errno对应于已知的系统错误代码,则为该错误构造适当的Errno类,否则构造通用的SystemCallError对象。 错误号码随后可通过errno方法获得。

代码语言:javascript
复制
static VALUE
syserr_initialize(int argc, VALUE *argv, VALUE self)
{
#if !defined(_WIN32)
    char *strerror();
#endif
    const char *err;
    VALUE mesg, error, func, errmsg;
    VALUE klass = rb_obj_class(self);

    if (klass == rb_eSystemCallError) {
        st_data_t data = (st_data_t)klass;
        rb_scan_args(argc, argv, "12", &mesg, &error, &func);
        if (argc == 1 && FIXNUM_P(mesg)) {
            error = mesg; mesg = Qnil;
        }
        if (!NIL_P(error) && st_lookup(syserr_tbl, NUM2LONG(error), &data)) {
            klass = (VALUE)data;
            /* change class */
            if (!RB_TYPE_P(self, T_OBJECT)) { /* insurance to avoid type crash */
                rb_raise(rb_eTypeError, "invalid instance type");
            }
            RBASIC_SET_CLASS(self, klass);
        }
    }
    else {
        rb_scan_args(argc, argv, "02", &mesg, &func);
        error = rb_const_get(klass, id_Errno);
    }
    if (!NIL_P(error)) err = strerror(NUM2INT(error));
    else err = "unknown error";

    errmsg = rb_enc_str_new_cstr(err, rb_locale_encoding());
    if (!NIL_P(mesg)) {
        VALUE str = StringValue(mesg);

        if (!NIL_P(func)) rb_str_catf(errmsg, " @ %"PRIsVALUE, func);
        rb_str_catf(errmsg, " - %"PRIsVALUE, str);
        OBJ_INFECT(errmsg, mesg);
    }
    mesg = errmsg;

    rb_call_super(1, &mesg);
    rb_ivar_set(self, id_errno, error);
    return self;
}

公共实例方法

errno → integer Show source

返回这个SystemCallError的错误号。

代码语言:javascript
复制
static VALUE
syserr_errno(VALUE self)
{
    return rb_attr_get(self, id_errno);
}

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com