Types of Errors
Types of Errors
In this tutorial, you will learn about different types of errors. A mistake made by a developer is known as an error. An error is a human action that produces an incorrect result and injects fault into the system. Errors can lead to failures if they are not identified and fixed. Understanding different types of errors is crucial in software development and testing.
Let’s explore the common types of errors developers encounter. Some of the types are as follows:
- Syntax Error
- Logical Error
- Runtime Error
Syntax Error
Syntax errors occur when the rules of a programming language are not followed correctly. These errors prevent the program from being compiled or interpreted. The program won’t run at all until these are fixed. For example, missing semicolon at the end of a statement in a C program. A C statement without a semicolon will lead to an error. Syntax errors are identified at the time of compiling the C program.
Examples
Java
print("Hello World" // Missing closing parenthesis
In the above example, the missing closing parenthesis will cause a syntax error.
JavaScript
Missing a semicolon in JavaScript:
console.log("Hello world") // Missing ';'
Logical Error
Logical errors occur when a program runs without crashing but produces incorrect or unexpected results. These errors are caused by mistakes in the logic of the code.
Examples:
Example: Incorrect operator
int add(int a, int b) { return a - b; // Incorrect logic, should be a + b }
The function is supposed to add two numbers but mistakenly subtracts them, leading to incorrect output.
Example: Incorrect area calculation
Calculating area of a circle with addition instead of multiplication:
area = π + radius² // Should be π * radius²
Runtime Error
Runtime errors occur during the execution of a program. These errors may cause the program to crash or behave unexpectedly. This is may due to invalid operations or external factors. For example, out of memory will cause run time error during the program run.
Example:
int divide(int a, int b) { return a / b; // If b is 0, this will cause a runtime error }
In this example, dividing by zero will result in a runtime error.