-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathError-Handling-10mins.py
More file actions
21 lines (14 loc) · 883 Bytes
/
Error-Handling-10mins.py
File metadata and controls
21 lines (14 loc) · 883 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# "Errors can crash programs, but Python’s try-except blocks help handle them gracefully."
# Basic Error Handling:
# Example Code:
try: # Try block
number = int(input("Enter a number: ")) # This line may raise a ValueError if input is not a number
print(10 / number) # This line may raise a ZeroDivisionError if number is zero
except ZeroDivisionError:
print("You cannot divide by zero!") # This block handles division by zero errors
except ValueError:
print("Please enter a valid number!") # This block handles invalid input errors (non-integer input)
finally:
print("Program finished.") # This block always executes, regardless of whether an exception was raised or not
# Student Activity:
# Write a program that divides two numbers and handles invalid inputs or division by zero.