Total Pageviews

Wednesday, September 9, 2026

Types of errors in programming

```html

⚠️ Types of Errors in Programming

Understanding Errors in Python Programming

📘 What is an Error?

An error is a problem in a program that prevents the program from executing correctly or producing the expected result. Errors can occur because of incorrect syntax, invalid operations, wrong data, or incorrect program logic.

In Python, errors are generally classified into Syntax Errors, Runtime Errors (Exceptions), and Logical Errors.

🔹 Major Types of Programming Errors

1. Syntax Error Violation of Python's grammar or syntax rules.
2. Runtime Error An error that occurs while the program is executing.
3. Logical Error Program executes but produces an incorrect result.
4. Semantic Error The statement may be syntactically valid but does not express the intended meaning.

1️⃣ Syntax Error

A Syntax Error occurs when the rules of the programming language are violated.

Example

print("Hello"
❌ Missing closing parenthesis ).

Another Example

if age >= 18 print("Adult")
❌ Missing colon : after the condition.

Correct Code

if age >= 18: print("Adult")
Output: Adult

2️⃣ Runtime Error

A Runtime Error occurs while the program is running. The syntax may be correct, but an invalid operation occurs during execution.

Example: Division by Zero

a = 10 b = 0 print(a / b)
ZeroDivisionError: division by zero

Python successfully understands the syntax, but division by zero is not a valid mathematical operation.

Example: Invalid List Index

numbers = [10, 20, 30] print(numbers[5])
IndexError: list index out of range

3️⃣ Common Runtime Errors / Exceptions

Exception Cause Example
ZeroDivisionError Division by zero 10 / 0
ValueError Invalid value int("abc")
TypeError Incompatible data types "10" + 5
NameError Variable is not defined print(x)
IndexError Invalid sequence index list[10]
KeyError Missing dictionary key dict["unknown"]
FileNotFoundError Requested file does not exist open("abc.txt")
AttributeError Invalid object attribute or method number.upper()

4️⃣ ValueError

A ValueError occurs when the data type is appropriate but the actual value is invalid.

age = int("hello") print(age)
ValueError: invalid literal for int()

5️⃣ TypeError

A TypeError occurs when an operation is performed on incompatible data types.

a = "10" b = 5 print(a + b)
TypeError: can only concatenate str to str

Correct Approach

a = "10" b = 5 print(int(a) + b)
15

6️⃣ Logical Error

A Logical Error occurs when the program runs without producing an error message, but the result is incorrect.

Example

a = 10 b = 20 average = a + b / 2 print(average)
20.0
⚠️ The expected average is 15, but the program produces 20.

Correct Code

a = 10 b = 20 average = (a + b) / 2 print(average)
15.0

There is no syntax or runtime error. The programmer simply used the wrong expression.

7️⃣ Semantic Error

A Semantic Error occurs when a statement is syntactically valid but its meaning does not match the programmer's intention.

Example

length = 10 width = 5 area = length + width print(area)
15
⚠️ The formula for the area of a rectangle should be: Area = length × width.

Correct Code

length = 10 width = 5 area = length * width print(area)
50

📊 Difference Between Major Errors

Error Type When Occurs? Program Executes? Example
Syntax Error Before execution ❌ No Missing :
Runtime Error During execution ❌ Stops at error 10 / 0
Logical Error During program design ✅ Yes Wrong formula
Semantic Error When meaning is incorrect ✅ Usually Wrong operation

🛡️ Handling Runtime Errors

Python provides try-except statements to handle exceptions and prevent abnormal termination of a program.

try: a = 10 b = 0 print(a / b) except ZeroDivisionError: print("Cannot divide by zero")
Cannot divide by zero

🔍 How to Find and Correct Errors?

  1. Read the error message carefully.
  2. Check the line number mentioned by Python.
  3. Check spelling of variables and functions.
  4. Check indentation.
  5. Check data types.
  6. Check mathematical formulas and logic.
  7. Use print statements to inspect values.
  8. Use a debugger when necessary.
  9. Test the program with different inputs.

✅ Advantages of Error Detection

  • Helps identify problems in programs.
  • Improves program reliability.
  • Makes debugging easier.
  • Prevents incorrect results.
  • Improves software quality.
  • Helps programmers understand incorrect code.

❌ Problems Caused by Errors

  • Program may terminate unexpectedly.
  • Incorrect results may be produced.
  • Debugging can take significant time.
  • Runtime errors can affect users.
  • Logical errors can be difficult to detect because the program runs.

🎯 Important Examination Points

Syntax Error
Runtime Error
Logical Error
Semantic Error
Exception
try-except
  • Syntax Error: Violation of language grammar.
  • Runtime Error: Occurs during program execution.
  • Logical Error: Program runs but gives wrong output.
  • ValueError: Correct type but inappropriate value.
  • TypeError: Operation performed on incompatible types.
  • ZeroDivisionError: Division by zero.
  • IndexError: Invalid list/string/tuple index.
  • NameError: Undefined variable or name.
  • try-except: Used for handling exceptions.

📌 Quick Summary

Error Meaning
Syntax Error Incorrect grammar/syntax
Runtime Error Problem during execution
Logical Error Incorrect result due to wrong logic
Semantic Error Incorrect meaning or intention
Remember: Syntax errors prevent execution, runtime errors occur during execution, while logical errors allow execution but produce an incorrect result.
```

No comments:

Post a Comment