GoldiptvPro

๐ŸŸข Lesson 5 โ€“ Intermediate Level

๐Ÿ”ฐ What Are Exceptions?

The current image has no alternative text. The file name is: IMG_20250810_014011_edit_664116464565327-scaled.jpg

An exception is an error that occurs during the execution of a program.
If not handled, it will stop the program and show an error message.

Example:

pythonCopierModifierprint(5 / 0)  # โŒ ZeroDivisionError

๐Ÿ›  Basic Error Handling with try and except

pythonCopierModifiertry:
    number = int(input("Enter a number: "))
    print(10 / number)
except ZeroDivisionError:
    print("โŒ You cannot divide by zero!")
except ValueError:
    print("โŒ Please enter a valid number.")

โž• Using else with try

pythonCopierModifiertry:
    num = int(input("Enter a number: "))
    result = 10 / num
except ZeroDivisionError:
    print("โŒ Cannot divide by zero.")
else:
    print("โœ… Division result:", result)

๐Ÿ“Œ else runs only if there is no error in try.


๐Ÿงน Using finally to Always Execute Code

pythonCopierModifiertry:
    file = open("example.txt", "r")
    print(file.read())
except FileNotFoundError:
    print("โŒ File not found.")
finally:
    print("๐Ÿ“Œ This runs no matter what.")

๐Ÿ“ค Raising Your Own Exceptions

pythonCopierModifierage = int(input("Enter your age: "))
if age < 0:
    raise ValueError("Age cannot be negative!")
else:
    print("Age is valid.")

๐Ÿ›  Custom Exception Class

pythonCopierModifierclass NegativeNumberError(Exception):
    pass

def check_number(num):
    if num < 0:
        raise NegativeNumberError("Negative numbers are not allowed.")
    else:
        print("Number is fine.")

try:
    check_number(-5)
except NegativeNumberError as e:
    print("โŒ Error:", e)

๐Ÿง  Exercise 1: Division Calculator with Error Handling

Write a program that:

  1. Asks the user for two numbers.
  2. Divides them.
  3. Handles:
    • ValueError if input is not a number.
    • ZeroDivisionError if denominator is zero.

๐Ÿง  Exercise 2: Safe File Reader

Ask the user for a file name:

  • If the file exists โ†’ print its content.
  • If not โ†’ print "File does not exist" and continue.

๐Ÿ“ Summary

  • Use try and except to catch and handle exceptions.
  • Use else for code that should only run if no error occurs.
  • Use finally for cleanup actions (closing files, releasing resources).
  • You can raise custom exceptions with raise.
  • You can create your own exception classes for specific situations.

Code Activate ูƒูˆุฏ ุงู„ุชูุนูŠู„ โฌ‡๏ธ Download GoldiptvPro ุชุญู…ูŠู„ Server 1 โฌ‡๏ธ Download GoldiptvPro ุชุญู…ูŠู„ Server 2

Related Posts

Rapid_tv

๐Ÿ”น Introduction The Rock, Paper, Scissors Game is a classic hand game that you can easily build in Python. ๐Ÿ‘‰ Rules: ๐Ÿ”น Code Example ๐Ÿ”น Example Run ๐Ÿ”น Concepts Learned…

Read more

VOD-ZalHD

๐Ÿ”น Introduction The Number Guessing Game is a classic beginner Python project. ๐Ÿ”น Code Example ๐Ÿ”น Example Run ๐Ÿ”น Concepts Learned ๐Ÿ”น Summary โฌ‡๏ธ Download VOD-ZalHD ุชุญู…ูŠู„ Server 1 โฌ‡๏ธ…

Read more

Delux_pro

๐Ÿ”น Introduction The To-Do List App is one of the most popular beginner projects. It teaches you how to: ๐Ÿ”น Code Example ๐Ÿ”น Features of This App ๐Ÿ”น Example Run…

Read more

Fam4k

๐Ÿ”ฐ Project Overview A calculator is one of the simplest yet most effective projects to start with in Python.You will learn: โœ๏ธ Step 1: Plan the Calculator We need: ๐Ÿ–ฅ๏ธ…

Read more

Speed_HD+

1. Introduction Data visualization helps us understand patterns and insights in data by converting raw numbers into charts and graphs.In Python, the most popular libraries are: 2. Installing Required Libraries…

Read more

Dauo

1. Introduction Web scraping is the process of extracting data from websites. In Python, we commonly use libraries like requests (to fetch web pages) and BeautifulSoup (to parse and extract…

Read more