Exception: Exception

Inherits:
Object show all
Defined in:
mruby/src/error.c,
mruby/mrblib/10error.rb

Overview

15.2.22

Instance Method Summary collapse

Constructor Details

#new(msg = nil) ⇒ Exception

Construct a new Exception object, optionally passing in

a message.


63
64
65
66
67
68
69
70
71
72
# File 'mruby/src/error.c', line 63

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

  if (mrb_get_args(mrb, "|o", &mesg) == 1) {
    mrb_exc_mesg_set(mrb, mrb_exc_ptr(exc), 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.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'mruby/src/error.c', line 87

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_exc_mesg_set(mrb, mrb_exc_ptr(exc), 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:



137
138
139
140
141
142
143
# File 'mruby/src/error.c', line 137

mrb_value
mrb_exc_inspect(mrb_state *mrb, mrb_value exc)
{
  mrb_value cname = mrb_mod_to_s(mrb, mrb_obj_value(mrb_obj_class(mrb, exc)));
  mrb_value mesg = mrb_exc_mesg_get(mrb, mrb_exc_ptr(exc)); /* string or nil */
  return (mrb_nil_p(mesg)||RSTRING_LEN(mesg)==0) ? cname : mrb_format(mrb, "%v (%v)", mesg, cname);
}

#messageObject

call-seq:

exception.message   ->  string

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



9
10
11
# File 'mruby/mrblib/10error.rb', line 9

def message
  to_s
end

#set_backtraceObject



167
168
169
170
171
172
173
174
# File 'mruby/src/error.c', line 167

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

  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:



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'mruby/src/error.c', line 111

static mrb_value
exc_to_s(mrb_state *mrb, mrb_value exc)
{
  mrb_value mesg = mrb_exc_mesg_get(mrb, mrb_exc_ptr(exc));
  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;
}