Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Programming by contract

 
luk Hann
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Does anybody feel familiar with the subject term? Relating it to an exception example: a square-root methd could require that it must be called only with a non-negtive argument. If called with a negative argument, the method would react by throwing an exception, since the contract between it and its caller has been broken. So how to express this contract in the code? What the code should be to demonstrate the above example? Thanks for any leads!
Luk
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, in java you CAN take the sqrt of a negative number, you just get back the value NaN (Not a Number). However if it DID throw an Exception you would code it like this.
Put it in a try catch block

So to give you a better example try inputting a string value, then revise the code to handle the problem.
 
luk Hann
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cindy,
Thanks much for your answer to my two questions. I highly appreciate!
luk
[This message has been edited by luk Hann (edited February 02, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic