Raise custom Exception with arguments

前端 未结 6 1777
礼貌的吻别
礼貌的吻别 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:06

    Here is a sample code adding a code to an error:

    class MyCustomError < StandardError
        attr_reader :code
    
        def initialize(code)
            @code = code
        end
    
        def to_s
            "[#{code}] #{super}"
        end
    end
    

    And to raise it: raise MyCustomError.new(code), message

提交回复
热议问题