contracts.ruby
contracts.ruby copied to clipboard
Document whether raising an exception violates contract
As a new user of contracts
, it's unclear to me by reading documentation and tutorials whether a contract is violated by raising an exception. Though it's relatively easy to figure out by trial and error, I'd appreciate a mention of this in your documentation.
Hi @philipmw. What do you mean exactly by "contract is violated by raising an exception", can you provide an example?
Either "Contract violation is an exception that is being raised" or "Raising exception will trigger contract violation" ? Which one of these?
Contract Num => Num
def kelvin_to_celsius(k)
raise 'Absolute temp cannot be negative!' if k < 0
k - 273.15
end
Here, if kelvin_to_celsius
raises an exception, will Contracts raise its own exception that kelvin_to_celsius
didn't return a number as advertised? My guess is no, since there doesn't appear to be a way to declare a contract involving what exceptions can be raised.
Ok. I see. Yes you are right, it will just raise normal exception and contracts will not bother in this case to even verify the result.
I can add this to an FAQ section when I break up the documentation.