Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"exception class/object expected" hash object cannot be rescued in ruby

begin
  hash = {"a" => "b"}
  raise hash
rescue Exception => e
  p e.message
end

Why i am not getting raised hash object in result, instead i am getting error - "exception class/object expected"

like image 974
Amith Avatar asked Nov 12 '16 04:11

Amith


1 Answers

Here's you're getting an error about the way that you're raising an error.

When you say raise you need to pass it an "exception class/object".

Some examples using built-in errors

raise(StandardError, "my message")

raise ArgumentError, "message"

raise NoMethodError

And creating a custom error class:

class MyError < StandardError
end

raise MyError, "message"
like image 163
max pleaner Avatar answered Sep 21 '22 15:09

max pleaner