Ruby has a fatal
exception, but there is no guidance on how to raise
it and I cannot figure it out. How do I raise a fatal
exception in Ruby?
Sure you can.
Try this
FatalError = ObjectSpace.each_object(Class).find { |klass| klass < Exception && klass.inspect == 'fatal' }
And then
raise FatalError.new("famous last words")
How does this work?
fatal
is an internal class without associated top-level constantObjectSpace.each_object(Class)
enumerates over all classes find { ... }
finds an exception class named "fatal"NB though, despite its name fatal
is not special, it can be rescued. If you are looking for a way to end your program maybe best call the global exit
method?
begin
raise FatalError.new
rescue Exception => e
puts "Not so fatal after all..."
end