1 / 56

Protecting Code: Misunderstandings, Mishaps, and Exceptions

Learn how to protect your code from misunderstandings, mishaps, and exceptions. This lecture covers defensive coding techniques and handling errors effectively. Slides are optimized for lecture delivery.

acotton
Download Presentation

Protecting Code: Misunderstandings, Mishaps, and Exceptions

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. Source: www.youtube.com/watch?v=bvLaTupw-hk‎

  3. ARIANE 5 launch

  4. Navigation instructions? Here’s an error message

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

  6. Putting up defences to protect our code. Your code Your teammate’s code CS2103/T, Lecture 7, Part 3, [Oct 2, 2015]

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

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

  9. MISUNDERSTANDINGS GO!

  10. MISUNDERSTANDINGS … int size = Config.getSize(); if( 0 < size < 5) setSizeAsSmall(); else setSizeAsBig(); … … if( sizeFound()) return readSize(); else return 0; … // 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)

  12. 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.

  13. MISUNDERSTANDINGS

  14. MISUNDERSTANDINGS GO!

  15. MISUNDERSTANDINGS GO!

  16. MISUNDERSTANDINGS MISHAPS GO! GO!

  17. 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))

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

  19. MISUNDERSTANDINGS MISHAPS Handle or raise an Exception

  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 (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

  25. 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

  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 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

  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. 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

  31. 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

  32. MISUNDERSTANDINGS MISHAPS GO! GO!

  33. MISUNDERSTANDINGS MISHAPS GO! GO!

  34. Your ToDo manager software tries to interact with Google calendar, but finds Google servers are down. This should be handled using, • Assertions • Exceptions • Depends google {a|b|c} e.g. google b 77577OR tinyurl.com/answerpost

  35. MISUNDERSTANDINGS MISHAPS MYSTERIES GO! GO! GO!

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

  37. 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”); …

  38. MISUNDERSTANDINGS MISHAPS MYSTERIES

  39. MISUNDERSTANDINGS MISHAPS MYSTERIES

  40. MISUNDERSTANDINGS MISHAPS MYSTERIES GO! GO! GO!

  41. MISUNDERSTANDINGS MISHAPS MYSTERIES GO! GO! GO!

  42. MISUSE MISUNDERSTANDINGS MISHAPS MYSTERIES GO! GO! GO! GO!

  43. class Man{ Woman girlfriend; void setGirlfriend(Woman w){ girlfriend = w; } … class Woman{ Man boyfriend; void setBoyfriend(Man m){ boyfriend = m; } … MISUSE MISUNDERSTANDINGS MISHAPS MYSTERIES girlfriend Man Woman boyfriend

  44. 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

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

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

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

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

  49. MISUSE MISUNDERSTANDINGS MISHAPS MYSTERIES

  50. MISUSE MISUNDERSTANDINGS MISHAPS MYSTERIES

More Related