-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrorHandling.py
More file actions
56 lines (46 loc) · 1.56 KB
/
errorHandling.py
File metadata and controls
56 lines (46 loc) · 1.56 KB
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
# for error handling in programs, it is good practice to use "try except" because it won't break the entirety of your code.
# if we didn't use "if except", the message 'This still prints' would not indeed print
try:
foobar
except NameError as err:
print(err)
print("This still prints.")
# this is a more comprehensive error handling process and runs while the input is not a number. The finally will still run after the break.
while True:
try:
print("Please enter a number.")
num = int(input())
except ValueError:
print("That's not a number.")
else:
# else runs when except does not
print("You entered a number!")
break
finally:
# this runs regardless
print("Runs regardless.")
# another example using division
def divide(a, b):
try:
return a/b
except ZeroDivisionError:
print("Numbers are not divisible by zero.")
except TypeError as:
print("a and b must be ints or floats")
print(err)
# using pdb (Python debugger) to set breakpoints
# this is not something that you keep in your code. It stays until you no longer need it.
import pdb; pdb.set_trace()
first = "First"
second = "Second"
pdb.set_trace()
result = first + second
third = "Third"
result += third
print(result)
# common pdb commands
# l for list, this will show you a map of the program with the breakpoint
# n for next line, this will move the debugger breakpoint to the next line
# p for print, use before the name of a variable to print that variable with its value
# c for continue, this will also have you exit debugging mode
# a to print all of the variables with their values