Basic Exception Handling
Basic Exception Handling
● Problem: Write a Python program that takes an integer input from the user and prints its
● Problem: Write a Python program to open a file named user_data.txt and read its
contents. Use exception handling to manage a FileNotFoundError if the file does not exist
3. Custom Exception
that asks the user for a number and raises this exception if the number is negative,
4. Multiple Exceptions
● Problem: Write a Python program that asks the user to input a number and calculates the
square root of the number. Your program should handle exceptions for at least two types
of errors: ValueError for non-numeric inputs and TypeError if a wrong data type is passed
5. Using finally
● Problem: Write a Python program that attempts to write data to a file and uses a finally
block to ensure that the file stream is closed, regardless of whether an exception occurs.
● Problem: Create a Python program with nested try statements: the outer block should
handle file operations, and the inner block should handle arithmetic operations. Use the
● Problem: Write a Python function that takes two integers and divides them. Use the assert
statement to ensure that the denominator is not zero before performing the division. Raise
● Problem: Write a Python program that prompts the user to enter an integer and then tries
to convert this input into an integer. Catch specific exceptions like ValueError if the input is
not an integer and a general exception to catch other unforeseen errors.
● Problem: Develop a Python program that accepts a list of numbers from the user and
prints the reciprocal of each. Use exception handling to specifically catch and handle the
cases of TypeError (if the user enters non-numeric types) and ZeroDivisionError (if the user
enters zero).
1. try:
reciprocal = 1 / number
except ZeroDivisionError:
except ValueError:
OUTPUT
Enter an integer: 5
Enter an integer: 0
2. try:
data = file.read()
print("File contents:")
print(data)
except FileNotFoundError:
File contents:
Hello, World!
3. class NegativeNumberError(Exception):
pass
try:
if number < 0:
except NegativeNumberError as e:
print(f"Error: {e}")
except ValueError:
OUTPUT
You entered 10
4. import math
try:
except ValueError:
except TypeError:
OUTPUT
5. try:
except IOError:
finally:
file.close()
print("File closed.")
OUTPUT
File closed.
6. try:
try:
number = int(file.readline())
result = 10 / number
print(f"Result of division: {result}")
except ZeroDivisionError:
except ValueError:
finally:
file.close()
except FileNotFoundError:
OUTPUT
7. try:
try:
number = int(file.readline())
result = 10 / number
except ZeroDivisionError:
except ValueError:
file.close()
except FileNotFoundError:
OUTPUT
Enter numerator: 10
Enter denominator: 2
Result: 5.0
Enter numerator: 10
Enter denominator: 0
8. try:
except ValueError:
except Exception as e:
Enter an integer: 42
You entered 42
OUTPUT
9. try:
numbers = input("Enter numbers separated by spaces: ").split()
try:
reciprocal = 1 / float(num)
except ZeroDivisionError:
except ValueError:
except Exception as e:
OUTPUT