forked from atheistpiece/ultimate-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exception_class.py
68 lines (51 loc) · 2.29 KB
/
exception_class.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class CustomError(Exception):
"""Custom class of errors.
This is a custom exception for any issues that arise in this module.
One of the reasons why developers design a class like this is for
consumption by downstream services and command-line tools.
If we designed a standalone application with no downstream consumers, then
it makes little sense to define a custom hierarchy of exceptions. Instead,
try using the existing hierarchy of builtin exception classes, which are
listed in the Python docs:
https://docs.python.org/3/library/exceptions.html
"""
class DivisionError(CustomError):
"""Any division error that results from invalid input.
This exception can be subclassed with the following exceptions if they
happen enough across the codebase:
- ZeroDivisorError
- NegativeDividendError
- NegativeDivisorError
That being said, there's a point of diminishing returns when we design
too many exceptions. It is better to design few exceptions that many
developers handle than design many exceptions that few developers handle.
"""
def divide_positive_numbers(dividend, divisor):
"""Divide a positive number by another positive number.
Writing a program in this style is what is considered defensive
programming. For more on this programming style, check out the
Wikipedia link below:
https://en.wikipedia.org/wiki/Defensive_programming
"""
if divisor == 0:
raise DivisionError("Cannot have a zero divisor")
elif dividend <= 0:
raise DivisionError("Cannot have a negative dividend")
elif divisor < 0:
raise DivisionError("Cannot have a negative divisor")
return dividend // divisor
def main():
# Exception classes are no different from concrete classes in that
# they all have inheritance baked in
assert issubclass(DivisionError, CustomError)
# Try a couple of inputs that are known to throw an error based on
# the exceptions thrown in `divide_positive_numbers`
for dividend, divisor in [(1, 0), (-1, 1), (1, -1)]:
try:
divide_positive_numbers(dividend, divisor)
except DivisionError as e:
print(e)
result = divide_positive_numbers(1, 1)
print(f"Divide(1, 1) = {result}")
if __name__ == '__main__':
main()