Exception: NestedError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/nested_error.rb

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.message # => "Something bad happened!"
  puts e.cause.message # => "divided by 0"
end

Direct Known Subclasses

AutomateIt::Error, HelpfulERB::Error

Instance Attribute Summary collapse

Instance Method Summary collapse

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(message, cause)
  self.cause = cause
  super(message)
end

Instance Attribute Details

#causeObject

Returns the value of attribute cause.



25
26
27
# File 'lib/nested_error.rb', line 25

def cause
  @cause
end