560 likes | 574 Views
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.
E N D
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.
Navigation instructions? Here’s an error message
Navigation instructions? Here’s an error message Your code Your teammate’s code
Putting up defences to protect our code. Your code Your teammate’s code CS2103/T, Lecture 7, Part 3, [Oct 2, 2015]
Putting up defences to protect our code. Your code Your teammate’s code
Lecture 7: Putting up defences to protect our code.
MISUNDERSTANDINGS … int size = Config.getSize(); if( 0 < size < 5) setSizeAsSmall(); else setSizeAsBig(); … … if( sizeFound()) return readSize(); else return 0; … // size > 0
MISUNDERSTANDINGS … int size = Config.getSize(); if( 0 < size < 5) setSizeAsSmall(); else setSizeAsBig(); … … if( sizeFound()) return readSize(); else return 0; … assert (size > 0)
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.
MISUNDERSTANDINGS MISHAPS GO! GO!
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))
MISUNDERSTANDINGS MISHAPS Developer User/environment User mishap bug? Handle or raise an Exception Verifyusing assertione.g. assert(isValid(age))
MISUNDERSTANDINGS MISHAPS Handle or raise an Exception
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
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
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
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
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
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
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
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
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
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
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
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
MISUNDERSTANDINGS MISHAPS GO! GO!
MISUNDERSTANDINGS MISHAPS GO! GO!
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
MISUNDERSTANDINGS MISHAPS MYSTERIES GO! GO! GO!
MISUNDERSTANDINGS MISHAPS MYSTERIES My computer crashes when I flush the toilet!
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”); …
MISUNDERSTANDINGS MISHAPS MYSTERIES
MISUNDERSTANDINGS MISHAPS MYSTERIES
MISUNDERSTANDINGS MISHAPS MYSTERIES GO! GO! GO!
MISUNDERSTANDINGS MISHAPS MYSTERIES GO! GO! GO!
MISUSE MISUNDERSTANDINGS MISHAPS MYSTERIES GO! GO! GO! GO!
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
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
Defensive driving can save your life Assume other drivers are idiots
career coding Defensive driving can save your life Assume other drivers are idiots coders
MISUSE MISUNDERSTANDINGS MISHAPS MYSTERIES class Man{ Woman girlfriend; void setGirlFriend(Woman w){ if(girlfriend!=null) girlfriend.breakupWith(this); girlfriend = w; girlfriend.setBoyFriend(this); } …
MISUSE MISUNDERSTANDINGS MISHAPS MYSTERIES
MISUSE MISUNDERSTANDINGS MISHAPS MYSTERIES