Reputation: 35
I am new to Ruby. What I understood from below code that a New class MyClass is created with in the ABC module. What #1 to #4 is doing. Is this throwing different exceptions which is a subtype of CommonError?
class ABC::MyClass
class AException < CommonError; end #1
class BException < CommonError; end #2
class CFailure < CommonError; end #3
class DException < CommonError; end #4
include ABC::Something
# ::::::::::::::::::::::::::::::::::::
end
class CommonError < Exception
end
Upvotes: 0
Views: 60
Reputation: 211740
That's just defining specific exceptions that can, presumably, be used within the code somewhere else, as in:
raise AException, "Something went wrong!"
This means you can rescue
those later:
begin
do_stuff!
rescue AException => e
puts "Uh oh, AException went off! Those are super bad!"
puts e # The message supplied in the raise call
end
The reason for CommonError
is to act as a base-class for all these other exceptions. The argument to rescue
is actually not a specific class, but a class and all subclasses, so if you rescue CommonError
you get to capture all of these and potentially others defined elsewhere.
Upvotes: 4