Costy
Costy

Reputation: 165

Ruby rescue custom exception

In following example:

class Foo
  class MyCustomerror < StandardError
    def message
      "My custom error"
    end
 end

 def self.do_standard
   1 / 0
 rescue StandardError => e
   puts e.message
 end

 def self.do_custom
   1 / 0
 rescue MyCustomerror => e
  puts e.message
 end
end

I have a problem with call rescue block which params is MyCustomerror. If i call Foo.do_standard, rescue block is called, however when i call Foo.do_custom rescue block with MyCustomerror isn't called. Where is the problem?

Upvotes: 0

Views: 594

Answers (1)

J&#246;rg W Mittag
J&#246;rg W Mittag

Reputation: 369428

There is no place in your code that could raise a MyCustomError exception, so there is nothing to rescue from. The only exception that could possibly be raised by that code is a ZeroDivisionError.

Upvotes: 2

Related Questions