Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoMethodError in Ruby :: Custom Exception class

I have a custom exception class as:

module ABC
  class XYZ < Exception

  end
end

I try to call my exception class in some other class as ::

raise ABC::XYZ "My Msg" if something != onething

I get the below exception:

NoMethodError: undefined method `XYZ' for ABC:Module
like image 729
ASingh Avatar asked Apr 22 '14 20:04

ASingh


2 Answers

You’re just missing a comma, the line should be:

raise ABC::XYZ, "My Msg" if something != onething

Currently it’s being parsed as a method call to XYZ with "My Msg" as the parameter, which gives the error since there is no XYZ method.

like image 177
matt Avatar answered Sep 22 '22 21:09

matt


You need to raise an instance of ABC::XYZ

As you have it, the Ruby interpreter assumes you are trying to execute a method.

raise ABC::XYZ.new("My Msg") if something != onething
like image 40
Kyle Avatar answered Sep 21 '22 21:09

Kyle