Search code examples
crystal-lang

Crystal get raised Exception's class


I was expecting that typeof(...) would give me the exact class, but when rescuing an Exception typeof(MyCustomException) just returns Exception

class A < Exception; end
class B < A; end

begin
  raise B.new
rescue e
  puts typeof(e) # => Exception
end

Whereas puts typeof(B.new) returns B as expected


Solution

  • As per https://github.com/bararchy on the issue I created here: https://github.com/crystal-lang/crystal/issues/4597

    I think that typeof is indeed Exception, but .class will give you what you want , I could be mistaken but I think that is intended
    

    So yeah e.class returns B