Programming Errors
Introduction to Programming Errors
- Programming errors, also known as bugs, are problems or defects in a computer program that prevent it from operating correctly.
- There are three main types of programming errors: syntax errors, runtime errors, and semantic (or logical) errors.
Syntax Errors
- Syntax errors are the most common type of programming error, occurring when the rules of the programming language are not followed.
- For example, forgetting to end a line of code with a semi-colon in languages where this is required will result in a syntax error.
- Syntax errors are typically caught during the process of compilation, where the computer is translating the high-level language into machine code.
Runtime Errors
- Runtime errors occur while the program is running, despite the program having correct syntax.
- This type of error typically involves operations that are impossible to carry out, like division by zero or trying to access an array element that does not exist.
- Runtime errors can often result in a program ‘crashing’ or stopping unexpectedly.
Semantic Errors
- Semantic errors, or logical errors, occur when a program compiles and runs without crashing, but doesn’t produce the right output.
- These are often the hardest to find and fix because they require understanding what the program is supposed to do versus what it is actually doing.
- A common example of a semantic error is wearing a coat when it gets dark, rather than when it gets cold. The program is doing what it was told perfectly, it’s just that what it was told to do doesn’t make sense in context.
Identifying and Correcting Errors
- In terms of identifying errors, syntax errors are often the easiest, as the compiler will specify the line with the error and usually also provide an error message.
- For runtime errors, the error message usually includes the type of error and location, but you may need to trace back to find what caused the issue.
- Semantic errors are often found through testing different inputs or scenarios in your program and needing to track down why the output is not what was expected.
Debugging
- Debugging is the process of identifying, isolating, and fixing errors in a program.
- Techniques for debugging can include ‘print’ debugging (adding print statements to track variable values), using a debugger program that allows you to step through the code line by line, or rubber duck debugging where you verbalise what the program is supposed to do.
Key Terms
- Programming Errors / Bugs: Problems or defects preventing a program from operating correctly.
- Syntax Errors: Errors resulting from not following the rules (syntax) of a programming language.
- Runtime Errors: Errors that occur while a program is running.
- Semantic / Logical Errors: Errors where a program runs but doesn’t give the correct output.
- Debugging: The process of identifying, isolating, and correcting problems in a program.