-
Notifications
You must be signed in to change notification settings - Fork 0
/
22-errors.py
58 lines (46 loc) · 1.66 KB
/
22-errors.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
#!/usr/local/bin/python3
import logging
# Default logging level is warning and above to console only
logging.basicConfig(filename="error.log", level=logging.DEBUG)
# In order of severity
# logging.critical("Very bad")
# logging.error("Bad")
# logging.warning("Not so bad")
# logging.info("Just thought you should know")
# logging.debug("What is going on")
def div(x, y):
result = 0
try:
f = open("output.txt", "a")
logging.info("Attempting division")
result = x / y
f.write("%i / %i = %i\n" % (x, y, result))
# If no exception type is specified, all exception types will be caught
# Multiple except blocks can catch multiple types
except ZeroDivisionError as obj:
print("Are you try to crash my program?")
logging.error(obj)
else:
print("Else block only executes if no exception was thrown.")
# Finally always executes, whether an exception was thrown or not
finally:
f.close()
div(4, 0)
userInput = int(input("Enter a number greater than 10: "))
# If false the assertion throws an exception, displays the message, and exits
# Assertions can be used in try/except blocks
assert userInput > 10, "You didn't follow directions."
def map(func, values):
output_values = []
index = 0
while index < len(values):
# Sets a breakpoint and drops into debugger command prompt
print("\nh for help, exit to leave the debugger\n")
breakpoint()
# or run code directly in debugger with: python3 -m pdb errors.py
output_values = func(values[index])
index += 1
return output_values
def add_one(val):
return val + 1
map(add_one, list(range(5)))