forked from joeyajames/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexception-handling.py
More file actions
31 lines (27 loc) · 812 Bytes
/
Copy pathexception-handling.py
File metadata and controls
31 lines (27 loc) · 812 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
31
filename = 'exception_data.txt'
# Outer try block catches file name or file doesn't exist errors.
try:
with open(filename) as fin:
for line in fin:
# print(line)
items = line.split(',')
total = 0
# Inner try bock catches integer conversion errors.
try:
for item in items:
num = int(item)
total += num
print('Total = ' + str(total))
except:
print('Error converting to integer. ', items)
except:
print('Error opening file. ' + filename)
finally:
print('This is our optional finally block. Code here will execute no matter what.')
# Second example: name Error type in except block; call function from try block.
def this_fails():
x = 1/0
try:
this_fails()
except ZeroDivisionError as err:
print('Handling run-time error:', err)