Download presentation
Presentation is loading. Please wait.
Published byRandolph Lindsey Modified over 9 years ago
2
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.here These slides contain a lot of animations. For optimal results, watch in slideshow mode.
3
Source: www.youtube.com/watch?v=bvLaTupw-hk
4
ARIANE 5 launch
5
Here’s an error message Navigation instructions?
6
Here’s an error message Navigation instructions? Your teammate’s code Your code
7
Your teammate’s code Putting up defences to protect our code. CS2103/T, Lecture 7, Part 3, [Oct 2, 2015]
8
Your code Your teammate’s code Putting up defences to protect our code.
9
Lecture 7: Putting up defences to protect our code.
10
GO!
11
… int size = Config.getSize(); if( 0 < size < 5) setSizeAsSmall(); else setSizeAsBig(); … if( sizeFound()) return readSize(); else return 0; … // size > 0
12
… int size = Config.getSize(); if( 0 < size < 5) setSizeAsSmall(); else setSizeAsBig(); … if( sizeFound()) return readSize(); else return 0; … assert (size > 0)
13
… 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.
15
GO!
18
What if age is invalid? void processAge(int age); //do stuff with age } bug? Handle or raise an Exception Verify using assertion e.g. assert(isValid(age)) void foo(String fileName); openFile(fileName); } User mishap
19
Handle or raise an Exception Developer User/environment bug? User mishap Verify using assertion e.g. assert(isValid(age))
20
Handle or raise an Exception
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) throws InvalidInputException { if(array.size()==0){ throw new InvalidInputException(“empty array”); } //process array }
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) throws InvalidInputException { if(array.size()==0){ throw new InvalidInputException(“empty array”); } //process array }
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) throws InvalidInputException { if(array.size()==0){ throw new InvalidInputException(“empty array”); } //process array }
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) throws InvalidInputException { if(array.size()==0){ throw new InvalidInputException(“empty array”); } //process array }
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) throws InvalidInputException { if(array.size()==0){ throw new InvalidInputException(“empty array”); } //process array }
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) throws InvalidInputException { if(array.size()==0){ throw new InvalidInputException(“empty array”); } //process array }
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) throws InvalidInputException { if(array.size()==0){ throw new InvalidInputException(“empty array”); } //process array }
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) throws InvalidInputException { if(array.size()==0){ throw new InvalidInputException(“empty array”); } //process array }
29
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) throws InvalidInputException { if(array.size()==0){ throw new InvalidInputException(“empty array”); } //process array }
30
Handle or raise an Exception public void processInput(String[] input) throws InvalidInputException { try { processArray(input); //do other things } catch (InvalidInputException e) { System.err.println(“Invalid input: " + e.getMessage()); throw e; } public void processArray(String[] array) throws InvalidInputException { if(array.size()==0){ throw new InvalidInputException(“empty array”); } //process array }
31
Handle or raise an Exception public void processInput(String[] input) throws InvalidInputException { try { processArray(input); //do other things } catch (InvalidInputException e) { System.err.println(“Invalid input: " + e.getMessage()); throw e; } public void processArray(String[] array) throws InvalidInputException { if(array.size()==0){ throw new InvalidInputException(“empty array”); } //process array }
32
Handle or raise an Exception public void processInput(String[] input) throws InvalidInputException { try { processArray(input); //do other things } catch (InvalidInputException e) { System.err.println(“Invalid input: " + e.getMessage()); throw e; } public void processArray(String[] array) throws InvalidInputException { if(array.size()==0){ throw new InvalidInputException(“empty array”); } //process array }
33
GO!
35
Your ToDo manager software tries to interact with Google calendar, but finds Google servers are down. This should be handled using, a)Assertions b)Exceptions c)Depends google {a|b|c} e.g. google b google {a|b|c} e.g. google b 77577 OR tinyurl.com/answerpost
36
GO!
37
My computer crashes when I flush the toilet!
38
… 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”); …
41
GO!
44
class Man{ Woman girlfriend; void setGirlfriend(Woman w){ girlfriend = w; } … class Woman{ Man boyfriend; void setBoyfriend(Man m){ boyfriend = m; } … Man Woman girlfriend boyfriend
45
class Man{ Woman girlfriend; void setGirlfriend(Woman w){ girlfriend = w; } … class Woman{ Man boyfriend; void setBoyfriend(Man m){ boyfriend = m; } … Man harry, ron; Woman hermione; ron.setGirlfriend(hermoine); hermione.setBoyfriend(harry); … ron:Man hermione :Woman harry:Man girlfriend boyfriend
46
45 What do you do when it is green light?
47
Defensive driving can save your life Assume other drivers are idiots
48
Defensive driving can save your life Assume other drivers are idiots coding career coders
49
class Man{ Woman girlfriend; void setGirlFriend(Woman w){ if(girlfriend!=null) girlfriend.breakupWith(this); girlfriend = w; girlfriend.setBoyFriend(this); } …
53
Bertrand Meyer, Design by Contract and the Eiffel Language
55
GO!
56
AssertionsExceptionsLoggingDefensive
57
GO! AssertionsExceptionsLoggingDefensive *some* evidence of each, by tutorial 8 (in 2 weeks)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.