My application (Ruby 1.9.2) may raise different exceptions, including net-connection breaks. I rescue Exception => e
, then do case/when
to handle them in defferent ways, but several errors go through my cases straight to else
.
rescue Exception => e
p e.class
case e.class
when Errno::ECONNRESET
p 1
when Errno::ECONNRESET,Errno::ECONNABORTED,Errno::ETIMEDOUT
p 2
else
p 3
end
end
Prints:
Errno::ECONNRESET
3
Well it depends upon whether you referencing the class or the constant. I have for instance had to use the following case statement to get a certain type of detection working
But that's because I'm working with the actual Exception Class not the constant. HTTPCLient is raising an actual class object:
Here's a puzzling fact:
Not sure what to make of that.
This is because of how the
===
operator works on the classClass
The
case
statement internally calls the===
method on the object you are evaluating against. If you want to test fore
class, you just test againste
, note.class
. That's becausee.class
would fall into thewhen Class
case, because, well, e.class is a Class.Yeah, Ruby can have weird semantics sometimes