Chris Keele Chris Keele - 4 months ago 15
Ruby Question

Raise custom Exception with arguments

I'm defining a custom Exception on a model in rails as kind of a wrapper Exception: (

begin[code]rescue[raise custom exception]end

When I raise the Exception, I'd like to pass it some info about a) the instance of the model whose internal functions raise the error, and b) the error that was caught.

This is going on an automated import method of a model that gets populated by POST request to from foreign datasource.

tldr; How can one pass arguments to an Exception, given that you define the Exception yourself? I have an initialize method on that Exception but the
syntax seems to only accept an Exception class and message, no optional parameters that get passed into the instantiation process.


create an instance of your exception with new:

class CustomException < StandardError
  def initialize(data)
    @data = data
# => nil 
raise "blupp")
# CustomException: CustomException