Types of Programming ErrorsI. Syntax ErrorsII. Fatal Run-Time Errors (Java "Exceptions")III. Logic ErrorsComputer Programming I Instructor: Greg ShawCOP 2210 Types of Programming Errors("To Err is Human, To Debug is Programming")All programming errors may be grouped into three categories based uponwhen (and if) they are detected.I. Syntax Errors- Definition: errors in grammar (violations of the "rules" forforming legal Java statements).- Detected by the Java compiler as it tries to translate yourprogram into the byte codes. I.e., to translate each .java fileinto a .class file- Examples: missing a semi-colon after a statement, mismatchedparentheses in an expression, an opening brace ({) without amatching closing brace (}), undeclared/misspelled identifiers- The Good News: The compiler will notify you of the location andthe nature of each syntax error. (However, understanding thecompiler's error messages takes some practice.)- The Good News 2: As you get more practice using a language, youwill naturally make fewer syntax errors, and will be able toquickly correct those that do occur!II. Fatal Run-Time Errors (Java "Exceptions")- Definition: "Asking the computer to do the impossible!"- Examples: division by zero, taking the square root of a negativenumber, referring to the 11th item on a list of only 10 items,dereferencing a null pointer (later), etc.- Occur when the compiled program is running ("executing").- In Java, fatal run-time errors are known as exceptions.- If an exception occurs and is not handled, the program willterminate abruptly ("crash").- Exception handling is covered in Programming II. This semester,we will concentrate on avoiding exceptions. - The Good News: The Java interpreter or run-time environment (aka:the JRE, the Java “virtual machine) will notify you of thelocation and nature of an exception should one occur.- The Good News 2: Avoiding exceptions and correcting programbehavior that "throws" exceptions is also largely a matter ofexperience. Also, there are antibugging (i.e., defensiveprogramming) techniques that we can use to prevent run-timeerrors from occurring.III. Logic Errors - Definition: the program compiles (no syntax errors) and runs to anormal completion (no exceptions thrown), but the output("results") is wrong! - Also known as semantic errors, these are errors in meaning. Astatement may be syntactically correct, but mean something otherthan what we intended. Therefor it has a different effect,causing the program output to be wrong.- Examples: using an incorrect formula to compute a value,performing arithmetic operations in the wrong order, improperinitialization of variables, etc.- The Bad News: Only you can determine whether or not a logic errorhas occurred, by careful examination of the program output.- Finding and correcting logic errors in a program is known asdebugging.- Debugging tips:1. Carefully hand-trace each statement of your program, writingdown all changes in the values of variables. This techniqueis known as "playing computer," and can help you find bugs.2. Insert temporary output statements into your program, so thatyou can check the values that are actually being stored inyour variables. These "diagnostic" output statements can beremoved later when the program is working correctly.WARNINGNEVER ASSUME THAT YOUR PROGRAM WORKS CORRECTLY JUSTBECAUSE IT COMPILES AND RUNS TO A NORMAL COMPLETION!ALWAYS CHECK YOUR OUTPUT WITH PENCIL, PAPER, AND ACALCULATOR (I.E., "PLAY COMPUTER") BEFORE HANDING INYOUR
View Full Document