Exception: NestedError
Overview
NestedError
An exception class that records the cause of another error. Useful when you need to raise a general kind of error, yet still be able to determine the underlying cause.
Example:
class MyGeneralError < NestedError; end
begin
begin
# Cause a specific error
1/0 # Divide by zero error
rescue Exception => e
# Wrap the specific error in a general, nested error
raise MyGeneralError("Something bad happened!", e)
end
rescue MyGeneralError => e
# Intercept the nested error and inspect the cause
puts e. # => "Something bad happened!"
puts e.cause. # => "divided by 0"
end
Direct Known Subclasses
Instance Attribute Summary collapse
-
#cause ⇒ Object
Returns the value of attribute cause.
Instance Method Summary collapse
-
#initialize(message, cause) ⇒ NestedError
constructor
Create a NestedObject with a
message
String and acause
Exception.
Constructor Details
#initialize(message, cause) ⇒ NestedError
Create a NestedObject with a message
String and a cause
Exception.
28 29 30 31 |
# File 'lib/nested_error.rb', line 28 def initialize(, cause) self.cause = cause super() end |
Instance Attribute Details
#cause ⇒ Object
Returns the value of attribute cause.
25 26 27 |
# File 'lib/nested_error.rb', line 25 def cause @cause end |