-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathexcept_excercise.py
More file actions
58 lines (46 loc) · 1.57 KB
/
except_excercise.py
File metadata and controls
58 lines (46 loc) · 1.57 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
55
56
57
58
#----------------------------------------#
# Title: except_excercise.py
# Initial File
# Claire Yoon,2019-02-09,New file
#----------------------------------------#
#!/usr/bin/python
"""
An exercise in playing with Exceptions.
Make lots of try/except blocks for fun and profit.
Make sure to catch specifically the error you find, rather than all errors.
"""
from except_test import fun, more_fun, last_fun
# Figure out what the exception is, catch it and while still
# in that catch block, try again with the second item in the list
first_try = ['spam', 'cheese', 'mr death']
try:
joke = fun(first_try[0])
except NameError as e:
joke = fun('cheese')
# Here is a try/except block. Add an else that prints not_joke
try:
not_joke = fun(first_try[2])
except SyntaxError:
print('Run Away!')
else:
print(not_joke)
# What did that do? You can think of else in this context, as well as in
# loops as meaning: "else if nothing went wrong"
# (no breaks in loops, no exceptions in try blocks)
# Figure out what the exception is, catch it and in that same block
#
# try calling the more_fun function with the 2nd language in the list,
# again assigning it to more_joke.
#
# If there are no exceptions, call the more_fun function with the last
# language in the list
# Finally, while still in the try/except block and regardless of whether
# there were any exceptions, call the function last_fun with no
# parameters. (pun intended)
langs = ['java', 'c', 'python']
try:
more_joke = more_fun(langs[0])
except Exception as e:
more_joke = more_fun(langs[1])
finally:
last_fun()