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

FlyFeditv

๐Ÿ“˜ Introduction In this project, youโ€™ll create a command-line Expense Tracker that allows users to: โœ… Add expenses with category, amount, and descriptionโœ… View total spending and category breakdownโœ… Automatically…

Read more

NetCinFly

๐Ÿ“˜ Introduction In this project, youโ€™ll develop a Student Grades Analyzer in Python that can: This project helps you strengthen your skills with lists, dictionaries, loops, and data analysis logic….

Read more

Eagle_Pro

๐ŸŒ Introduction In this project, youโ€™ll create a Web Scraper App in Python that extracts quotes, authors, and tags from a live website.Youโ€™ll use the BeautifulSoup and Requests libraries to…

Read more

Cobra_Pro

๐ŸŒฆ๏ธ Introduction In this project, youโ€™ll build a Weather App in Python that retrieves live weather information from an online API.Youโ€™ll learn how to work with HTTP requests, JSON data,…

Read more

Show7-Pro

Concepts Covered: ๐ŸŽฏ Objective: Create a simple, text-based contact book application that allows users to: ๐Ÿ’ก Code (contact_book.py): ๐Ÿง  Example Usage Console Output Example: ๐Ÿ’พ Notes โฌ‡๏ธ Download Show7-Pro ุชุญู…ูŠู„…

Read more

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