Tigriptv

Introduction

The current image has no alternative text. The file name is: ESLA.jpg

Errors are a normal part of programming. In Python, you will encounter two main types of errors:

  1. Syntax Errors – mistakes in the structure of your code.
  2. 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 TypeExample CodeExplanation
SyntaxErrorprint("Hello"Missing parenthesis.
ZeroDivisionError10 / 0Cannot divide by zero.
NameErrorprint(x) (x not defined)Using a variable that doesn’t exist.
TypeError"5" + 2Mixing incompatible data types.
ValueErrorint("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.
  • else runs only if no error, finally always 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

Related Posts

Cleo_patra

1. Introduction File handling is one of the most important skills in Python. Almost every real-world application needs to read from or write to a file. Python makes file handling…

Read more

Hyperhd

📌 What are Regular Expressions (Regex)? Regular Expressions (regex) are powerful tools used to search, match, and manipulate text patterns. Python provides the re module to work with regex. 🔹…

Read more

GO-tv

Introduction JSON (JavaScript Object Notation) is a lightweight format for storing and exchanging data. It’s widely used in APIs, configuration files, and data storage. Python has a built-in json module…

Read more

Eagle

Lesson 9: Dictionaries in Depth 1. Introduction to Dictionaries Dictionaries are one of the most powerful and flexible data structures in Python.They store data as key-value pairs and are extremely…

Read more

NovaPlayer

Lesson 8: Lists, Tuples, and Sets in Python 1. What is a Dictionary in Python? A dictionary in Python is a collection of key-value pairs. 📌 Example: 2. Creating Dictionaries…

Read more

Akratv

Lesson 7 – Functions and Scope in Python 📘 Introduction In Python, lists, tuples, and sets are three important ways to store collections of items. Each has unique features and…

Read more