0% found this document useful (0 votes)
8 views2 pages

Lab 5

This document covers error handling in Python, focusing on exception handling techniques such as try-except blocks, raising custom exceptions, and creating custom exception classes. It includes code implementations for handling division by zero, multiple exceptions, and a custom exception for negative numbers. The conclusion emphasizes the importance of error handling for developing safe and robust applications.

Uploaded by

metrobooksjmc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Lab 5

This document covers error handling in Python, focusing on exception handling techniques such as try-except blocks, raising custom exceptions, and creating custom exception classes. It includes code implementations for handling division by zero, multiple exceptions, and a custom exception for negative numbers. The conclusion emphasizes the importance of error handling for developing safe and robust applications.

Uploaded by

metrobooksjmc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Lab 5: Error Handling in Python

Objective
- To understand how to detect and handle runtime errors using exception handling techniques in
Python, including built-in exceptions and custom-defined exceptions.

Theory
Errors in programming are common and can cause a program to stop unexpectedly. Python provides a
way to handle errors gracefully using try-except blocks, so the program doesn't crash.

 try: Code that might cause an error.


 except: Code that runs if an error occurs.
 finally: (Optional) Runs no matter what.
 raise: Used to raise a custom exception.
 We can also create our own exception class by extending the Exception class.

Code Implementations:
1. Handle division by zero exception.

- try:
- a = int(input("Enter numerator: "))
- b = int(input("Enter denominator: "))
- result = a / b
- print("Result:", result)
- except ZeroDivisionError:
- print("Error: Cannot divide by zero.")

2. Handle multiple exceptions (File Not Found, Index Out of Range, etc.).

- try:
- file = open("data.txt", "r")
- content = file.read()
- print(content)
- numbers = [1, 2, 3]
- print(numbers[5])
-
- except FileNotFoundError:
- print("Error: File not found.")
- except IndexError:
- print("Error: Index is out of range.")
- except Exception as e:
- print("An unexpected error occurred:", e)

3. Custom exception class.

- class NegativeNumberError(Exception):
- pass
-
- def check_positive(num):
- if num < 0:
- raise NegativeNumberError("Negative numbers are not allowed.")
- else:
- print("You entered:", num)
-
- try:
- n = int(input("Enter a positive number: "))
- check_positive(n)
- except NegativeNumberError as e:
- print("Custom Error:", e)

Conclusion
In this lab, we learned how to use Python’s error-handling features to catch and manage exceptions like
division by zero, file not found, and index out of range. We also created a custom exception class,
which helps in handling specific errors more meaningfully. Error handling is essential for building safe
and robust applications.

You might also like