Re-enable spec/library for full CI runs.
[rbx.git] / kernel / core / exception.rb
blobcd77f66d9b0bc068926ef47f011848541f367471
1 # depends on: class.rb
3 class Exception
4   
5   ivar_as_index :__ivars => 0, :message => 1, :context => 2
6   def message; @message ; end
8   def initialize(message = nil)
9     @message = message
10     @context = nil
11     @backtrace = nil
12   end
14   def backtrace
15     return @backtrace if @backtrace
16     return nil unless @context
17     awesome_backtrace.to_mri
18   end
19   
20   def awesome_backtrace
21     return nil unless @context
22     @backtrace = Backtrace.backtrace(@context)
23   end
25   def set_backtrace(bt)
26     @backtrace = bt
27   end
28   
29   def to_s
30     @message || self.class.to_s
31   end
32   
33   def inspect
34     "#<#{self.class.name}: #{self.to_s}>"
35   end
37   alias_method :message, :to_s
38   alias_method :to_str, :to_s
39   
40   def self.exception(message=nil)
41     self.new(message)
42   end
43   
44   def exception(message=nil)
45     if message
46       self.class.new(message)
47     else 
48       self
49     end
50   end
51   
52   def context
53     @context
54   end
56   def context=(other)
57     @context = other
58   end
59   
60   def location
61     ctx = self.context
62     [ctx.file.to_s, ctx.line]
63   end
64 end
67 # Primitive fails from opcode "send_primitive"
69 class PrimitiveFailure < Exception
70 end
72 class ScriptError < Exception
73 end
75 class StandardError < Exception
76 end
78 class SignalException < Exception
79 end
81 class NoMemoryError < Exception
82 end
84 class ZeroDivisionError < StandardError
85 end
87 class ArgumentError < StandardError
88 end
90 class IndexError < StandardError
91 end
93 class RangeError < StandardError
94 end
96 class FloatDomainError < RangeError
97 end
99 class LocalJumpError < StandardError
102 class NameError < StandardError
103   attr_reader :name
104   def initialize(*args)
105     super(args.shift)
106     @name = args.shift
107   end
110 class NoMethodError < NameError
111   attr_reader :name
112   attr_reader :args
113   def initialize(*arguments)
114     super(arguments.shift)
115     @name = arguments.shift
116     @args = arguments.shift
117   end
120 class RuntimeError < StandardError
123 class SecurityError < StandardError
126 class SystemStackError < StandardError
129 class ThreadError < StandardError
132 class TypeError < StandardError
135 class FloatDomainError < RangeError
138 class RegexpError < StandardError
141 class LoadError < ScriptError
144 class NotImplementedError < ScriptError
147 class Interrupt < SignalException
150 class IOError < StandardError
153 class EOFError < IOError
156 class LocalJumpError < StandardError
159 class NotImplementedError < ScriptError
162 class SyntaxError < ScriptError
163   attr_accessor :column
164   attr_accessor :line
165   attr_accessor :file
166   attr_accessor :code
168   def import_position(c,l, code)
169     @column = c
170     @line = l
171     @code = code
172   end
174   def message
175     msg = super
176     msg = "#{file}:#{@line}: #{msg}" if file && @line
177     msg
178   end
181 class SystemCallError < StandardError
182   def errno; @errno ; end
184   def initialize(message, errno = nil)
185     if message.is_a?(Integer) && errno.nil?
186       errno   = message
187       message = "Unknown error"
188     end
189     super(message)
190     @errno = errno
191   end
195 # Raised when you try to return from a block when not allowed.  Never seen by
196 # ruby code.
198 class IllegalLongReturn < LocalJumpError
199   attr_reader :return_value
203 # Abstract class for implementing flow control in Rubinius.  Never seen by
204 # ruby code.
206 class FlowControlException < Exception
210 # Flow control exception used to implement return inside an ensure.  Never
211 # seen by ruby code.
213 class ReturnException < FlowControlException
214   attr_reader :return_value
216   def initialize(val)
217     super(nil) # HACK
218     @return_value = val
219   end
223 # Raised when returning from a block to handle proper flow control.  Never
224 # seen by ruby code.
226 class LongReturnException < FlowControlException
227   attr_reader :value
228   attr_reader :is_return
230   def return_value=(val)
231     @value = val
232     @is_return = true
233   end
235   def break_value=(val)
236     @value = val
237     @is_return = false
238   end