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

FivG_Pro

๐Ÿ”ฐ What Is a Function? A function is a reusable block of code that performs a specific task.Instead of repeating code, we can write it once and call it whenever…

Read more

Show7_Pro

๐ŸŸข Lesson 6 โ€“ Intermediate Level ๐Ÿ”ฐ What Is a Module? A module is simply a Python file (.py) containing code (functions, variables, classes) you can reuse in other programs….

Read more

Domingo

๐ŸŸข Lesson 4 ๐Ÿ”ฐ What Is OOP? Object-Oriented Programming (OOP) is a programming style where you organize code into objects that contain: Python supports OOP using: ๐Ÿงฑ Example: Why Use…

Read more

SHARK_PRO

๐Ÿ”ฐ What Is File Handling? ๐ŸŸข Lesson 3 File handling lets your Python programs interact with files on your computer โ€” read data from them or save data to them….

Read more

ZinaTv

๐ŸŸข Lesson 2 โ€“ Intermediate Level ๐Ÿ”น Python Dictionaries: Storing and Accessing Key-Value Data ๐Ÿ”ฐ What Is a Dictionary? A dictionary in Python is a data structure that stores data…

Read more

Pro_Click

๐Ÿ”ฐ What Are Lists ? A list is an ordered collection of items stored in a single variable. ๐Ÿ“Œ Lists are defined using square brackets []. ๐Ÿงช Example : pythonCopierModifierfruits…

Read more