Question 57: Write a Python program to create user defined exception that will check whether the password is correct or not. Download hole Program / Project code, by clicking following link: How to handle exception in Python ? Exception handling in Python is done using try, except, else, and finally blocks. Here's how you can handle exceptions in Python:
Commonly, exceptions can be categorized into two main types: - Try Block: You enclose the code that might raise an exception within a try block.
try:
# Code that may raise an exception
result = 10 / 0 # This will raise a ZeroDivisionError
except:
# Handle the exception here
print("An exception occurred") - Except Block: You specify one or more except blocks immediately after the try block to catch and handle specific exceptions. You can catch specific exception types or use a more general except block to catch any exception.
try:
result = 10 / 0
except ZeroDivisionError:
print("Division by zero is not allowed")
except ArithmeticError:
print("An arithmetic error occurred")
except:
print("An exception occurred") - Else Block (Optional): You can include an else block after all except blocks. The code in the else block will run only if no exceptions are raised in the try block.
try:
result = 10 / 2
except ZeroDivisionError:
print("Division by zero is not allowed")
else:
print("No exception occurred. Result:", result) - Finally Block (Optional): You can include a finally block after all other blocks. The code in the finally block will run regardless of whether an exception was raised.
try:
result = 10 / 0
except ZeroDivisionError:
print("Division by zero is not allowed")
finally:
print("This block always runs, even if an exception occurred") - Specific Exception Handling: For more precise exception handling, you can catch specific exception types. This allows you to provide custom error messages and handle different exceptions differently.
try:
result = 10 / 0
except ZeroDivisionError:
print("Division by zero is not allowed")
except ValueError:
print("A value error occurred") - Exception Object: You can capture the exception object and its message using the as keyword.
try:
result = 10 / 0
except ZeroDivisionError as e:
print("An exception occurred:", e) - Raising Exceptions: You can raise your own exceptions using the raise statement to indicate that an error has occurred in your code.
try:
age = int(input("Enter your age: "))
if age < 0:
raise ValueError("Age cannot be negative")
except ValueError as e:
print("Error:", e)
Exception handling is crucial for writing robust and reliable code. It allows you to gracefully recover from errors, provide informative error messages, and ensure that your program doesn't crash when unexpected issues arise.
Programming Code: Following code write in: BP_P57.py # Python Program
# User Define Exception
class InvalidAgeException(Exception):
"This Raised when the input value is less than 18(age)."
pass
age = 18
try:
num = int(input("Enter a Integer value: "))
if num < age:
raise InvalidAgeException
else:
print("Eligible to Vote.")
except InvalidAgeException:
print("Exception Occurred: Invalid Age.")
# Thanks For Reading.
Output:
Commonly, exceptions can be categorized into two main types:
- Try Block: You enclose the code that might raise an exception within a try block.
try:
# Code that may raise an exception
result = 10 / 0 # This will raise a ZeroDivisionError
except:
# Handle the exception here
print("An exception occurred") - Except Block: You specify one or more except blocks immediately after the try block to catch and handle specific exceptions. You can catch specific exception types or use a more general except block to catch any exception.
try:
result = 10 / 0
except ZeroDivisionError:
print("Division by zero is not allowed")
except ArithmeticError:
print("An arithmetic error occurred")
except:
print("An exception occurred") - Else Block (Optional): You can include an else block after all except blocks. The code in the else block will run only if no exceptions are raised in the try block.
try:
result = 10 / 2
except ZeroDivisionError:
print("Division by zero is not allowed")
else:
print("No exception occurred. Result:", result) - Finally Block (Optional): You can include a finally block after all other blocks. The code in the finally block will run regardless of whether an exception was raised.
try:
result = 10 / 0
except ZeroDivisionError:
print("Division by zero is not allowed")
finally:
print("This block always runs, even if an exception occurred") - Specific Exception Handling: For more precise exception handling, you can catch specific exception types. This allows you to provide custom error messages and handle different exceptions differently.
try:
result = 10 / 0
except ZeroDivisionError:
print("Division by zero is not allowed")
except ValueError:
print("A value error occurred") - Exception Object: You can capture the exception object and its message using the as keyword.
try:
result = 10 / 0
except ZeroDivisionError as e:
print("An exception occurred:", e) - Raising Exceptions: You can raise your own exceptions using the raise statement to indicate that an error has occurred in your code.
try:
age = int(input("Enter your age: "))
if age < 0:
raise ValueError("Age cannot be negative")
except ValueError as e:
print("Error:", e)
# Python Program # User Define Exception class InvalidAgeException(Exception): "This Raised when the input value is less than 18(age)." pass age = 18 try: num = int(input("Enter a Integer value: ")) if num < age: raise InvalidAgeException else: print("Eligible to Vote.") except InvalidAgeException: print("Exception Occurred: Invalid Age.") # Thanks For Reading.
Output:
0 Comments