-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_exceptions.py
60 lines (50 loc) · 1.08 KB
/
my_exceptions.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
"""
Learn about exceptions
"""
import sys
def convert(s):
"""
Converts a string to integer
:param s:
:return:
"""
try:
return int(s)
except (ValueError, TypeError) as e:
print("Conversion error {}"\
.format(str(e)), file=sys.stderr)
return -1
def sqrt(x):
"""
Compute the sqrt using the method of Heron of Alexandria
:param x: Number to compute the sqrt
:return: The sqrt of x
:raise ValueError() on ZeroDivisionError
"""
guess = x
i = 0
try:
while guess*guess != x and i < 20:
guess = (guess + x/guess)/2.0
i += 1
except ZeroDivisionError:
raise ValueError()
return guess
def main():
"""
Test function
:return:
"""
# print(convert("11"))
# print(convert("Hello"))
# print(convert([1, 4, 8]))
try:
print(sqrt(9))
print(sqrt(11))
print(sqrt(-1))
except ValueError:
print("Cannot compute the value of negative numbers")
print("Done")
if __name__ == '__main__':
main()
exit(0)