Section 2.4: Errors
Common errors ● Mismatched parentheses ● Omitting space after operator or between numbers ● Putting operator between operands
Tracking Mistakes ● Computer programs never work right on the first try ● Need to analyze errors – Symptoms – Diagnosis – Treatment – Prevention
Symptoms ● Error message; if so, read it! ● Wrong answers; if so, look for patterns – answer always wrong in the same way? – answer right on some inputs, wrong on others? ● No answers at all
Diagnosis What did I do to cause the error? ● Matching parentheses, braces, quotation marks, etc. ● Misspellings & typoes ● Calling function w/wrong number or wrong type of arguments ● Wrong formula ● Inadequate test cases ● etc. ● Free-form comments
Treatment & prevention ● For mismatched parentheses, quotation marks, misspellings, etc. obvious ● If fixing the problem took thought, write down how you fixed it
Analyzing Errors in Math In math class… 1) do the problem 2) pray that you got it right 3) check the back of the book 4) “aw, shucks, I got it wrong” 5) go on to the next problem anyway ● This won’t work in a programming class! ● Nothing will work right on the first try! ● Prayer won’t help much.
Analyzing Errors in Programming In programming class… 1) do the problem 2) pray that you got it right 3) test it 4) “aw, shucks, I got it wrong” 5) Identify how it was wrong 6) Find something in your program that could cause that 7) Fix it 8) go back to step 3
Kinds of Errors ● Syntax (i.e. grammar) – DrScheme “Check Syntax” catches, and gives helpful error messages, highlighting where program is wrong – Examples: misspellings, mismatched parentheses ● Run-time error messages – DrScheme “Run” catches, and gives sorta-helpful error messages, with a guess at where program is wrong – Examples: wrong number of arguments, wrong type arguments, division by zero ● Logical error messages (aka “bugs”) – No error message; you have to recognize that the answer (or behavior) is wrong. You have to figure out where. – Examples: + where you meant *, <= where you meant <, wrong order…
Exercise 2.4.2: Syntax Errors ● The following are all illegal programs in Scheme because of a syntax mistake. ● Type in the program and execute it. ● You will get an error message. ● Fix something and execute again. ● Repeat until there is no error messages. ● Then type in the next program and repeat.
Exercise 2.4.2: Syntax Errors (define (f 1) (+ x 10)) (define g x) + x 10) (define h(x) (+ x 10))
Exercise 2.4.2: Syntax Errors (define f 1) (+ x 10)); the number 1 cannot be a variable (define g x) + x 10) (define h(x) (+ x 10))
Exercise 2.4.2: Syntax Errors (define f 1) (+ x 10)); the number 1 cannot be a variable (define g x) + x 10); there is no left parenthesis next to ; operator + (define h(x) (+ x 10))
Exercise 2.4.2: Syntax Errors (define f 1) (+ x 10)); the number 1 cannot be a variable (define g x) + x 10); there is no left parenthesis next to ; operator + (define h(x) (+ x 10)); do not include the variable x in ; parentheses
Exercise 2.4.4: Run-time Errors ● Enter the following grammatically legal Scheme program into the Definitions window. (define (somef x) (sin x x)) ● Click Run. ● Then, in the Interactions window, evaluate: (somef 10 20) (somef 10) ● Read the error messages. ● Also observe what DrScheme highlights.
Exercise 2.4.4: Run-time Errors ● Enter the following grammatically legal Scheme program into the Definitions window. (define (somef x) (sin x x)) ● Click Run. ● Then, in the Interactions window, evaluate: (somef 10 20) somef: this procedure expects 1 argument, here it is provided 2 arguments (somef 10) sin: expects 1 argument, given 2: ● Read the error messages. ● Also observe what DrScheme highlights.
Logical Errors Logical errors (aka “bugs”) are the hardest to fix. To prevent them: ● Choose good test cases in advance – At least one for each “category” of input or output – If input involves ranges of numbers, test borderline cases – Find as many bugs as you can; if you don’t find them, I will! ● Look for patterns of wrong answers: – All answers are wrong in the same way (e.g. negative or reciprocal of right answer) – Borderline cases are wrong – Borderline cases are right; everything else is wrong – Simplest cases are right; more complicated ones are all wrong ● Use Stepper to spot where program “goes off the rails”
In summary… ● A syntax error is caused by not following all the rules when writing a Scheme function. ● A runtime error is caused when an example or test case is not consistent with its corresponding Scheme function (either the function or the example may be wrong). ● A logical error means that you have followed all the rules of Scheme, but your Scheme function doesn’t make sense in real life. An example, is writing a Scheme function that divides the length and width to find the area.