1 / 53

WARNING

WARNING. These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010. They may not show up well on other PowerPoint versions . You can download PowerPoint 2010 viewer from here .

creda
Download Presentation

WARNING

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. WARNING • These slides are not optimized for printing or exam preparation. These are for lecture delivery only. • These slides are made for PowerPoint 2010. They may not show up well on other PowerPoint versions. You can download PowerPoint 2010 viewer from here. • These slides contain a lot of animations. For optimal results, watch in slideshow mode.

  2. ARIANE 5 launch

  3. Navigation instructions? Here’s an error message

  4. Navigation instructions? Here’s an error message Your code Your teammate’s code

  5. Putting up defences to protect our code. Your code Your teammate’s code CS2103/T, Lecture 7, Part 3, [Oct 4, 2013]

  6. Putting up defences to protect our code. Your code Your teammate’s code

  7. Lecture 7: Putting up defences to protect our code.

  8. MISUNDERSTANDINGS GO!

  9. MISUNDERSTANDINGS … int size = Config.getSize(); if( 0 < size < 5) setSizeAsSmall(); else setSizeAsBig(); … … if( sizeFound()) return readSize(); else return 0; … // size > 0

  10. MISUNDERSTANDINGS … int size = Config.getSize(); if( 0 < size < 5) setSizeAsSmall(); else setSizeAsBig(); … … if( sizeFound()) return readSize(); else return 0; … assert (size > 0)

  11. MISUNDERSTANDINGS … int size = Config.getSize(); if( 0 < size < 5) setSizeAsSmall(); else setSizeAsBig(); … … if( sizeFound()) return readSize(); else return 0; … assert (size > 0) Make your assumptions explicit. Get the runtime to confirm them.

  12. MISUNDERSTANDINGS

  13. MISUNDERSTANDINGS GO!

  14. MISUNDERSTANDINGS GO!

  15. MISUNDERSTANDINGS MISHAPS GO! GO!

  16. MISUNDERSTANDINGS MISHAPS What if age is invalid? void processAge(intage); //do stuff with age } User mishap bug? void foo(String fileName); openFile(fileName); } Handle or raise an Exception Verifyusing assertione.g. assert(isValid(age))

  17. MISUNDERSTANDINGS MISHAPS Developer User/environment User mishap bug? Handle or raise an Exception Verifyusing assertione.g. assert(isValid(age))

  18. MISUNDERSTANDINGS MISHAPS Handle or raise an Exception

  19. Handle or raise an Exceptions • public void processInput(String[] input) { • try { • processArray(input); • //do other things • } catch (InvalidInputException e) { • System.err.println(“Invalid input: " • + e.getMessage()); • } • } • public void processArray(String[] array) throwsInvalidInputException { • if(array.size()==0){ • thrownew InvalidInputException(“empty array”); • } • //process array • } MISUNDERSTANDINGS MISHAPS

  20. Handle or raise an Exceptions • public void processInput(String[] input) { • try { • processArray(input); • //do other things • } catch (InvalidInputException e) { • System.err.println(“Invalid input: " • + e.getMessage()); • } • } • public void processArray(String[] array) throwsInvalidInputException { • if(array.size()==0){ • thrownew InvalidInputException(“empty array”); • } • //process array • } MISUNDERSTANDINGS MISHAPS

  21. Handle or raise an Exceptions • public void processInput(String[] input) { • try { • processArray(input); • //do other things • } catch (InvalidInputException e) { • System.err.println(“Invalid input: " • + e.getMessage()); • } • } • public void processArray(String[] array) throwsInvalidInputException { • if(array.size()==0){ • thrownew InvalidInputException(“empty array”); • } • //process array • } MISUNDERSTANDINGS MISHAPS

  22. Handle or raise an Exceptions • public void processInput(String[] input) { • try { • processArray(input); • //do other things • } catch (InvalidInputException e) { • System.err.println(“Invalid input: " • + e.getMessage()); • } • } • public void processArray(String[] array) throwsInvalidInputException{ • if(array.size()==0){ • thrownew InvalidInputException(“empty array”); • } • //process array • } MISUNDERSTANDINGS MISHAPS

  23. Handle or raise an Exceptions • public void processInput(String[] input) { • try { • processArray(input); • //do other things • }catch (InvalidInputException e) { • System.err.println(“Invalid input: " • + e.getMessage()); • } • } • public void processArray(String[] array) throwsInvalidInputException { • if(array.size()==0){ • thrownew InvalidInputException(“empty array”); • } • //process array • } MISUNDERSTANDINGS MISHAPS

  24. Handle or raise an Exceptions • public void processInput(String[] input) { • try { • processArray(input); • //do other things • } catch (InvalidInputExceptione) { • System.err.println(“Invalid input: " • + e.getMessage()); • } • } • public void processArray(String[] array) throwsInvalidInputException{ • if(array.size()==0){ • thrownew InvalidInputException(“empty array”); • } • //process array • } MISUNDERSTANDINGS MISHAPS

  25. Handle or raise an Exceptions • public void processInput(String[] input) { • try { • processArray(input); • //do other things • } catch (InvalidInputException e) { • System.err.println(“Invalid input: " • + e.getMessage()); • } • } • public void processArray(String[] array) throwsInvalidInputException { • if(array.size()==0){ • thrownew InvalidInputException(“empty array”); • } • //process array • } MISUNDERSTANDINGS MISHAPS

  26. Handle or raise an Exceptions • public void processInput(String[] input) { • try { • processArray(input); • //do other things • } catch (InvalidInputException e) { • System.err.println(“Invalid input: " • + e.getMessage()); • } • } • public void processArray(String[] array) throwsInvalidInputException { • if(array.size()==0){ • thrownew InvalidInputException(“empty array”); • } • //process array • } MISUNDERSTANDINGS MISHAPS

  27. Handle or raise an Exceptions • public void processInput(String[] input) { • try { • processArray(input); • //do other things • } catch (InvalidInputException e) { • System.err.println(“Invalid input: " • + e.getMessage()); • } • } • public void processArray(String[] array) throwsInvalidInputException { • if(array.size()==0){ • thrownew InvalidInputException(“empty array”); • } • //process array • } MISUNDERSTANDINGS MISHAPS

  28. Handle or raise an Exception • public void processInput(String[] input) throwsInvalidInputException { • try { • processArray(input); • //do other things • } catch (InvalidInputException e) { • System.err.println(“Invalid input: " • + e.getMessage()); • throw e; • } • } • public void processArray(String[] array) throwsInvalidInputException { • if(array.size()==0){ • thrownew InvalidInputException(“empty array”); • } • //process array • } MISUNDERSTANDINGS MISHAPS

  29. Handle or raise an Exception • public void processInput(String[] input) throwsInvalidInputException { • try { • processArray(input); • //do other things • } catch (InvalidInputException e) { • System.err.println(“Invalid input: " • + e.getMessage()); • throw e; • } • } • public void processArray(String[] array) throwsInvalidInputException { • if(array.size()==0){ • thrownew InvalidInputException(“empty array”); • } • //process array • } MISUNDERSTANDINGS MISHAPS

  30. MISUNDERSTANDINGS MISHAPS GO! GO!

  31. MISUNDERSTANDINGS MISHAPS GO! GO!

  32. Your ToDo manager software tries to interact with Google calendar, but finds Google servers are down. This should be handled using, • Assertions • Exceptions • Depends goog {a|b|c} e.g. goog b 77577

  33. MISUNDERSTANDINGS MISHAPS MYSTERIES GO! GO! GO!

  34. MISUNDERSTANDINGS MISHAPS MYSTERIES My computer crashes when I flush the toilet!

  35. MISUNDERSTANDINGS MISHAPS MYSTERIES … int size = Config.getSize(); // size > 0 if( 0 < size < 5) log(LEVEL_INFO, “Setting size to small”); setSizeAsSmall(); else if( size > 5) setSizeAsBig(); else log(LEVEL_WARN, “size not recognized”); …

  36. MISUNDERSTANDINGS MISHAPS MYSTERIES

  37. MISUNDERSTANDINGS MISHAPS MYSTERIES

  38. MISUNDERSTANDINGS MISHAPS MYSTERIES GO! GO! GO!

  39. MISUNDERSTANDINGS MISHAPS MYSTERIES GO! GO! GO!

  40. MISUSE MISUNDERSTANDINGS MISHAPS MYSTERIES GO! GO! GO! GO!

  41. class Man{ Woman girlfriend; void setGirlFriend(Woman w){ girlfriend = w; } … class Woman{ Man boyfriend; void setBoyFriend(Man m){ boyfriend = m; } … MISUSE MISUNDERSTANDINGS MISHAPS MYSTERIES Man harry, ron; Woman hermione; ron.setGirlFriend(hermoine); hermione.setBoyFriend(harry); … ron:Man harry:Man boyfriend hermione:Woman girlfriend

  42. What do you do when it is green light?

  43. Defensive driving can save your life Assume other drivers are idiots

  44. career coding Defensive driving can save your life Assume other drivers are idiots coders

  45. MISUSE MISUNDERSTANDINGS MISHAPS MYSTERIES class Man{ Woman girlfriend; void setGirlFriend(Woman w){ if(girlfriend!=null) girlfriend.breakupWith(this); girlfriend = w; girlfriend.setBoyFriend(this); } …

  46. MISUSE MISUNDERSTANDINGS MISHAPS MYSTERIES

  47. MISUSE MISUNDERSTANDINGS MISHAPS MYSTERIES

  48. MISUSE MISUNDERSTANDINGS MISHAPS MYSTERIES

  49. Bertrand Meyer, Design by Contract and the Eiffel Language MISUSE MISUNDERSTANDINGS MISHAPS MYSTERIES

  50. MISUSE MISUNDERSTANDINGS MISHAPS MYSTERIES

More Related