forked from wadehuber/codeexamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexceptions.py
More file actions
30 lines (25 loc) · 696 Bytes
/
Copy pathexceptions.py
File metadata and controls
30 lines (25 loc) · 696 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"""
File: exceptions.py
This file demonstrates how to handle exceptions in Python.
It includes examples of catching specific exceptions and using
the `raise` statement to propagate exceptions with additional context.
"""
def safe_divide(a, b):
"""
Safely divide two numbers.
Args:
a (int): The numerator.
b (int): The denominator.
Raises:
ValueError: If the denominator is zero.
Returns:
float: The result of the division.
"""
try:
return a / b
except ZeroDivisionError as e:
raise ValueError("Cannot divide by zero") from e
try:
print(safe_divide(10, 0))
except ValueError as err:
print("ERROR:", err)