example-custom-exceptions.py 894 Bytes
Newer Older
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
#!/usr/bin/env python
from __future__ import print_function
import sys
sys.path.append('.')

import example

print("Can we catch a MyException?")
try:
    example.throws1()
except example.MyException as e:
    print(e.__class__.__name__, ":", e)
print("")

print("Can we translate to standard Python exceptions?")
try:
    example.throws2()
except Exception as e:
    print(e.__class__.__name__, ":", e)
print("")

print("Can we handle unknown exceptions?")
try:
    example.throws3()
except Exception as e:
    print(e.__class__.__name__, ":", e)
print("")

print("Can we delegate to another handler by rethrowing?")
try:
    example.throws4()
except example.MyException as e:
    print(e.__class__.__name__, ":", e)
print("")

print("Can we fall-through to the default handler?")
try:
    example.throws_logic_error()
except Exception as e:
    print(e.__class__.__name__, ":", e)
print("")