Skip to content

Commit 7ebe9d5

Browse files
committed
Adds examples of Exceptions.
1 parent ebf871e commit 7ebe9d5

File tree

6 files changed

+193
-0
lines changed

6 files changed

+193
-0
lines changed
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python
2+
"""Example of raising an exception through a call chain.
3+
4+
Created on Aug 19, 2011
5+
6+
@author: paulross
7+
"""
8+
9+
def a():
10+
b()
11+
12+
def b():
13+
c()
14+
15+
def c():
16+
raise Exception('Ouch')
17+
18+
def main():
19+
a()
20+
21+
if __name__ == '__main__':
22+
main()

Beginers/ExampleExceptions/B_catch.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env python
2+
"""Example of raising an exception through a call chain.
3+
Function c() will randomly raise a type ExceptionOne or ExceptionTwo. These
4+
will be caught by a() and b() respectively.
5+
6+
Created on Aug 19, 2011
7+
8+
@author: paulross
9+
"""
10+
11+
import random
12+
13+
class ExceptionOne(Exception):
14+
pass
15+
16+
class ExceptionTwo(Exception):
17+
pass
18+
19+
def a():
20+
try:
21+
b()
22+
print(' a() called b() with no exception raised')
23+
except ExceptionOne as err:
24+
print(' a() CAUGHT %s' % err)
25+
26+
def b():
27+
try:
28+
c()
29+
except ExceptionTwo as err:
30+
print(' b() CAUGHT %s' % err)
31+
else:
32+
print(' b() No exception raised')
33+
34+
def c():
35+
# randVal is (0, 1, 2)
36+
randVal = random.randint(0, 2)
37+
if randVal == 1:
38+
raise ExceptionOne('I am the One')
39+
elif randVal == 2:
40+
raise ExceptionTwo('I am the One two')
41+
# Do something. Hopefully its useful.
42+
43+
def main():
44+
for i in range(8):
45+
print('Round %d:' % i)
46+
a()
47+
return 0
48+
49+
if __name__ == '__main__':
50+
main()
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env python
2+
"""Example of raising an exception where b() has a finally clause and a()
3+
catches the exception.
4+
5+
Created on Aug 19, 2011
6+
7+
@author: paulross
8+
"""
9+
10+
class ExceptionNormal(Exception):
11+
pass
12+
13+
class ExceptionCleanUp(Exception):
14+
pass
15+
16+
def a():
17+
try:
18+
b()
19+
except ExceptionNormal as err:
20+
print(' a(): CAUGHT "%s"' % err)
21+
22+
def b():
23+
try:
24+
c()
25+
finally:
26+
print(' b(): finally: This code is always executed.')
27+
28+
def c():
29+
print('Raising "ExceptionNormal" from c()')
30+
raise ExceptionNormal('ExceptionNormal raised from function c()')
31+
32+
def main():
33+
a()
34+
return 0
35+
36+
if __name__ == '__main__':
37+
main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python
2+
"""Illustrates the problem where a finally block raises an exception. This
3+
can mask the original exception that caused the problem in the first place.
4+
5+
The user sees the ExceptionCleanUp but not the ExceptionNormal which was masked
6+
(in Python 2.x). Python 3 reports both exceptions.
7+
8+
Created on Aug 19, 2011
9+
10+
@author: paulross
11+
"""
12+
13+
class ExceptionNormal(Exception):
14+
pass
15+
16+
class ExceptionCleanUp(Exception):
17+
pass
18+
19+
def a():
20+
b()
21+
22+
def b():
23+
try:
24+
c()
25+
finally:
26+
print(' b(): finally: This code is always executed.')
27+
cleanUp()
28+
29+
def c():
30+
print('Raising "ExceptionNormal" from c()')
31+
raise ExceptionNormal('ExceptionNormal raised from function c()')
32+
33+
def cleanUp():
34+
raise ExceptionCleanUp('Can not clean up')
35+
36+
def main():
37+
a()
38+
return 0
39+
40+
if __name__ == '__main__':
41+
main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python
2+
"""Illustrates one possible solution to the problem where a finally block raises
3+
an exception.
4+
5+
Created on Aug 19, 2011
6+
7+
@author: paulross
8+
"""
9+
10+
class ExceptionNormal(Exception):
11+
pass
12+
13+
class ExceptionCleanUp(Exception):
14+
pass
15+
16+
def a():
17+
b()
18+
19+
def b():
20+
try:
21+
c()
22+
finally:
23+
# If doing something that might raise then trap this exception
24+
# to preserve the original one (if any).
25+
try:
26+
print(' b(): finally: This code is always executed.')
27+
cleanUp()
28+
except Exception as err:
29+
print(' -- HELP! Consuming: %s' % type(err))
30+
31+
def c():
32+
print('Raising "ExceptionNormal" from c()')
33+
raise ExceptionNormal('ExceptionNormal raised from function c()')
34+
35+
def cleanUp():
36+
raise ExceptionCleanUp('Can not clean up')
37+
38+
def main():
39+
a()
40+
return 0
41+
42+
if __name__ == '__main__':
43+
main()

Beginers/ExampleExceptions/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)