Raise custom Exception with arguments

前端 未结 6 1805
礼貌的吻别
礼貌的吻别 2020-12-23 13:36

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 Exc

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-23 14:21

    create an instance of your exception with new:

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

提交回复
热议问题