contracts.ruby icon indicating copy to clipboard operation
contracts.ruby copied to clipboard

Document whether raising an exception violates contract

Open philipmw opened this issue 9 years ago • 5 comments

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.

philipmw avatar Aug 05 '15 20:08 philipmw

Hi @philipmw. What do you mean exactly by "contract is violated by raising an exception", can you provide an example?

waterlink avatar Aug 05 '15 20:08 waterlink

Either "Contract violation is an exception that is being raised" or "Raising exception will trigger contract violation" ? Which one of these?

waterlink avatar Aug 05 '15 20:08 waterlink

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.

philipmw avatar Aug 05 '15 20:08 philipmw

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.

waterlink avatar Aug 05 '15 20:08 waterlink

I can add this to an FAQ section when I break up the documentation.

egonSchiele avatar Aug 05 '15 22:08 egonSchiele