Quantcast
Channel: Ruby custom error classes: inheritance of the message attribute - Stack Overflow
Viewing all articles
Browse latest Browse all 5

Answer by Stefan for Ruby custom error classes: inheritance of the message attribute

$
0
0

raise already sets the message so you don't have to pass it to the constructor:

class MyCustomError < StandardError  attr_reader :object  def initialize(object)    @object = object  endendbegin  raise MyCustomError.new("an object"), "a message"rescue MyCustomError => e  puts e.message # => "a message"  puts e.object # => "an object"end

I've replaced rescue Exception with rescue MyCustomError, see Why is it a bad style to `rescue Exception => e` in Ruby?.


Viewing all articles
Browse latest Browse all 5

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>