I'm re-posting this GitHub pull request from ariejan [1] and my
response in case it helps anyone else :-
> I have the problem that I cannot rescue (in my own code) an exception raised by Mocha. Also see my gist at
http://gist.github.com/239578
>
> I've added two tests that 1) test that an exception is actually raised 2) a test that proves that a rescue block does not capture the exception.
>
> Hopefully, this makes sense to anyone. Feel free to contact me for more info.
>
> Regards,
> Ariejan
By calling "raises(Exception)", you are explicitly requesting that
Mocha raises an instance of the Exception class.
Your rescue clause "rescue => e" is shorthand for "rescue
StandardError => e".
StandardError is a sub-class of Exception, but not the other way
around.
This means that Mocha is correctly raising an instance of Exception
and your rescue clause is not rescuing it (as I would expect).
If you want to keep your rescue clause unchanged (probably the most
sensible option), you should change the call to "raises()" or "raises
(StandardError)".
If you actually want to rescue instances of Exception (rarely a good
idea), you should change your rescue clause to "rescue Exception =>
e".
I don't see anything wrong with Mocha's behaviour in this case. Please
let me know if I've missed something.
Cheers, James.
[1]
http://github.com/ariejan