I have seen Ruby code that raises exceptions using a class:
raise GoatException, "Maximum of 3 goats per bumper car."
Other code uses an instance:
raise GoatException.new "No leotard found suitable for goat."
Both of these are rescued the same way. Is there any reason to use an instance vs a class?
It makes no difference; the exception class will be instiantiated in either case.
If you provide a string, either as the argument to
newor as the second argument toraise, it be passed toinitializeand will become the exception instance’s.message.For example:
If you comment the first
raiseabove, you’ll see that:initializeis called.GoatException, notclassas it would be if we were rescuing the exception class itself.