Exception: Exception

Inherits:
Object show all
Defined in:
error.c

Overview

Descendents of class Exception are used to communicate between raise methods and rescue statements in begin/end blocks. Exception objects carry information about the exception—its type (the exception’s class name), an optional descriptive string, and optional traceback information. Programs may subclass Exception to add additional information.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#new(msg = nil) ⇒ Exception

Construct a new Exception object, optionally passing in

a message.


348
349
350
# File 'error.c', line 348

static VALUE
exc_initialize(argc, argv, exc)
int argc;

Class Method Details

.new(args, ...) ⇒ Object

Calls allocate to create a new object of class’s class, then invokes that object’s initialize method, passing it args. This is the method that ends up getting called whenever an object is constructed using .new.

Returns:



1585
1586
1587
# File 'object.c', line 1585

VALUE
rb_class_new_instance(argc, argv, klass)
int argc;

Instance Method Details

#backtraceArray

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

Returns:



487
488
489
# File 'error.c', line 487

static VALUE
exc_backtrace(exc)
VALUE exc;

#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.to_str.



376
377
378
# File 'error.c', line 376

static VALUE
exc_exception(argc, argv, self)
int argc;

#inspectString

Return this exception’s class name an message

Returns:



436
437
438
# File 'error.c', line 436

static VALUE
exc_inspect(exc)
VALUE exc;

#messageString #to_strString

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.

Overloads:



422
423
424
# File 'error.c', line 422

static VALUE
exc_to_str(exc)
VALUE exc;

#set_backtrace(array) ⇒ Array

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

Returns:



530
531
532
# File 'error.c', line 530

static VALUE
exc_set_backtrace(exc, bt)
VALUE exc;

#to_sString

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

Returns:



400
401
402
# File 'error.c', line 400

static VALUE
exc_to_s(exc)
VALUE exc;

#messageString #to_strString

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.

Overloads:



422
423
424
# File 'error.c', line 422

static VALUE
exc_to_str(exc)
VALUE exc;