Introduction

Tiger_iptv
Errors are a normal part of programming. In Python, you will encounter two main types of errors:
- Syntax Errors – mistakes in the structure of your code.
- Exceptions – errors that occur while the program is running.
Python provides a powerful way to handle exceptions so your program doesn’t crash.
Common Errors in Python
| Error Type | Example Code | Explanation |
|---|---|---|
| SyntaxError | print("Hello" | Missing parenthesis. |
| ZeroDivisionError | 10 / 0 | Cannot divide by zero. |
| NameError | print(x) (x not defined) | Using a variable that doesn’t exist. |
| TypeError | "5" + 2 | Mixing incompatible data types. |
| ValueError | int("abc") | Invalid value for type conversion. |
Basic Exception Handling
try:
number = int(input("Enter a number: "))
result = 10 / number
print("Result:", result)
except ZeroDivisionError:
print("Error: You cannot divide by zero.")
except ValueError:
print("Error: Please enter a valid number.")
✅ If the user enters 0, it catches ZeroDivisionError.
✅ If the user enters "abc", it catches ValueError.
Using else and finally
try:
num = int(input("Enter a number: "))
print("Square:", num ** 2)
except ValueError:
print("Invalid input. Enter a number.")
else:
print("Operation successful!") # runs if no error
finally:
print("Program ended.") # always runs
- else: Runs only if no exception occurs.
- finally: Runs no matter what (useful for cleanup).
Raising Exceptions
You can raise exceptions manually using raise.
age = -5
if age < 0:
raise ValueError("Age cannot be negative")
Summary
- Syntax Errors stop the code from running.
- Exceptions happen during execution.
- Use try/except to handle errors gracefully.
elseruns only if no error,finallyalways runs.- You can raise your own exceptions.
Error handling makes programs more robust, safe, and user-friendly.
⬇️ Download Tigriptv تحميل Server 1 ⬇️ Download Tigriptv تحميل Server 2 ⬇️ Download Tigriptv تحميل Server 3