forked from gregmalcolm/python_koans
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabout_exceptions.py
More file actions
68 lines (52 loc) · 1.69 KB
/
about_exceptions.py
File metadata and controls
68 lines (52 loc) · 1.69 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
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from runner.koan import *
class AboutExceptions(Koan):
class MySpecialError(RuntimeError):
pass
def test_exceptions_inherit_from_exception(self):
mro = self.MySpecialError.__mro__
self.assertEqual(__, mro[1].__name__)
self.assertEqual(__, mro[2].__name__)
self.assertEqual(__, mro[3].__name__)
self.assertEqual(__, mro[4].__name__)
def test_try_clause(self):
result = None
try:
self.fail("Oops")
except StandardError as ex:
result = 'exception handled'
self.assertEqual(__, result)
self.assertEqual(____, isinstance(ex, StandardError))
self.assertEqual(____, isinstance(ex, RuntimeError))
self.assertTrue(issubclass(RuntimeError, StandardError), \
"RuntimeError is a subclass of StandardError")
self.assertEqual(__, ex[0])
def test_raising_a_specific_error(self):
result = None
try:
raise self.MySpecialError, "My Message"
except self.MySpecialError as ex:
result = 'exception handled'
self.assertEqual(__, result)
self.assertEqual(__, ex[0])
def test_else_clause(self):
result = None
try:
pass
except RuntimeError:
result = 'it broke'
pass
else:
result = 'no damage done'
self.assertEqual(__, result)
def test_finally_clause(self):
result = None
try:
self.fail("Oops")
except:
# no code here
pass
finally:
result = 'always run'
self.assertEqual(__, result)