exception handlin
exception handlin
### 2. Exceptions
- Definition : Exceptions are specific types of runtime errors that are "raised" by Python when it encounters
an unexpected condition.
- Characteristics :
- They are a subset of runtime errors in Python, specifically designed to be caught and managed through
exception handling (`try`, `except`).
- When an exception occurs, Python raises an error object, which can then be handled using `except`
blocks.
- If an exception is not handled, it will terminate the program and show an error traceback.
- Examples :
- ZeroDivisionError : Raised when trying to divide by zero.
- ValueError : Raised when a function receives an argument of the correct type but an invalid value.
- FileNotFoundError : Raised when attempting to open a file that doesn’t exist.
exceptions are a subset of runtime errors in Python that can be managed using `try-except` blocks, allowing
programs to handle errors gracefully.
Not all runtime errors are exceptions, but all exceptions are runtime errors.
Exception handling allows a program to manage errors gracefully, helping avoid unexpected crashes. Python
uses the try, except, else, and finally blocks to handle exceptions.
1. ZeroDivisionError
Example:
result = 10 / 0
print("Error: You cannot divide by zero.")
2. TypeError
result = "hello" + 5
print(result)
Output:
3. ValueError
Occurs when a function receives an argument of the correct type but an invalid value.
Example:
number = int("hello")
print(number)
Output:
4. FileNotFoundError
Example:
Output:
5. IndexError
Example:
my_list = [1, 2, 3]
element = my_list[5]
print(element)
Output:
Syntax:
python
try:
# Code that might raise an exception
except ExceptionType:
# Code that runs if the specified exception occurs
Example:
python
try:
num = int(input("Enter a number: "))
print("Result:", 10 / num)
except ZeroDivisionError:
print("Error: You cannot divide by zero.")
except ValueError:
print("Error: Please enter a valid integer.")
Output Examples:
If user enters 0:
Enter a number: 0
Error: You cannot divide by zero.
Using try-except-else
The else block runs only if no exception occurs in the try block.
Syntax:
try:
# Code that might raise an exception
except ExceptionType:
# Code to handle the exception
else:
# Code that runs if no exception occurs
Example:
python
try:
num = int(input("Enter a number: "))
result = 10 / num
except ZeroDivisionError:
print("Error: You cannot divide by zero.")
else:
print("Result:", result)
Output Examples:
If user enters 2:
Enter a number: 2
Result: 5.0
If user enters 0:
Enter a number: 0
Error: You cannot divide by zero.
The finally block is used to execute code regardless of whether an exception occurs or not. This is helpful
for cleanup tasks like closing files or releasing resources.
Syntax:
python
try:
# Code that might raise an exception
except ExceptionType:
# Code to handle the exception
finally:
# Code that runs regardless of exceptions
Example:
python
j
Using try, except, else, and finally blocks provides a way to make programs robust and prevent unexpected
crashes.