Exception: Exception

Inherits:
Object show all
Defined in:
src/error.c

Overview

15.2.22

Instance Method Summary collapse

Constructor Details

#new(msg = nil) ⇒ Exception

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


43
44
45
46
47
48
49
50
51
52
53
54
# File 'src/error.c', line 43

static mrb_value
exc_initialize(mrb_state *mrb, mrb_value exc)
{
  mrb_value mesg;
  mrb_int argc;
  mrb_value *argv;

  if (mrb_get_args(mrb, "|o*!", &mesg, &argv, &argc) >= 1) {
    mrb_iv_set(mrb, exc, mrb_intern_lit(mrb, "mesg"), mesg);
  }
  return exc;
}

Instance Method Details

#backtraceObject

#exceptionObject

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.


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'src/error.c', line 69

static mrb_value
exc_exception(mrb_state *mrb, mrb_value self)
{
  mrb_value exc;
  mrb_value a;
  mrb_int argc;

  argc = mrb_get_args(mrb, "|o", &a);
  if (argc == 0) return self;
  if (mrb_obj_equal(mrb, self, a)) return self;
  exc = mrb_obj_clone(mrb, self);
  mrb_iv_set(mrb, exc, mrb_intern_lit(mrb, "mesg"), a);

  return exc;
}

#inspectString

Returns this exception’s file name, line number, message and class name. If file name or line number is not set, returns message and class name.

Returns:


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'src/error.c', line 133

static mrb_value
exc_inspect(mrb_state *mrb, mrb_value exc)
{
  mrb_value str, mesg, file, line;
  mrb_bool append_mesg;
  const char *cname;

  mesg = mrb_attr_get(mrb, exc, mrb_intern_lit(mrb, "mesg"));
  file = mrb_attr_get(mrb, exc, mrb_intern_lit(mrb, "file"));
  line = mrb_attr_get(mrb, exc, mrb_intern_lit(mrb, "line"));

  append_mesg = !mrb_nil_p(mesg);
  if (append_mesg) {
    mesg = mrb_obj_as_string(mrb, mesg);
    append_mesg = RSTRING_LEN(mesg) > 0;
  }

  cname = mrb_obj_classname(mrb, exc);
  str = mrb_str_new_cstr(mrb, cname);
  if (mrb_string_p(file) && mrb_fixnum_p(line)) {
    if (append_mesg) {
      str = mrb_format(mrb, "%S:%S: %S (%S)", file, line, mesg, str);
    }
    else {
      str = mrb_format(mrb, "%S:%S: %S", file, line, str);
    }
  }
  else if (append_mesg) {
    str = mrb_format(mrb, "%S: %S", str, mesg);
  }
  return str;
}

#messageString

Returns the result of invoking exception.to_s. Normally this returns the exception’s message or name.

Returns:


117
118
119
120
121
# File 'src/error.c', line 117

static mrb_value
exc_message(mrb_state *mrb, mrb_value exc)
{
  return mrb_funcall(mrb, exc, "to_s", 0);
}

#set_backtraceObject


187
188
189
190
191
192
193
194
195
# File 'src/error.c', line 187

static mrb_value
exc_set_backtrace(mrb_state *mrb, mrb_value exc)
{
  mrb_value backtrace;

  mrb_get_args(mrb, "o", &backtrace);
  set_backtrace(mrb, exc, backtrace);
  return backtrace;
}

#to_sString

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

Returns:


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'src/error.c', line 93

static mrb_value
exc_to_s(mrb_state *mrb, mrb_value exc)
{
  mrb_value mesg = mrb_attr_get(mrb, exc, mrb_intern_lit(mrb, "mesg"));
  struct RObject *p;

  if (!mrb_string_p(mesg)) {
    return mrb_str_new_cstr(mrb, mrb_obj_classname(mrb, exc));
  }
  p = mrb_obj_ptr(mesg);
  if (!p->c) {
    p->c = mrb->string_class;
  }
  return mesg;
}