Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathsampleexceptions.py
More file actions
Latest commit
90 lines (65 loc) · 2.23 KB
/
Copy pathsampleexceptions.py
File metadata and controls
90 lines (65 loc) · 2.23 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# python sampleexceptions.py
# Minimal types of exceptions are used in this sample.
# For a list of built in python exceptions see https://docs.python.org/2/library/exceptions.html
# Expected Output:
#
# Basic exception raised.
# Basic exception with message: ERROR.
# Caught non basic exception: No module named badimport.
# Caught non basic exception: unsupported operand type(s) for +: 'int' and 'str'.
# Demonstrated how to handle multiple types of exceptions the same way.
# Demonstrated how to handle multiple types of exceptions the same way with a message: No module named badimport.
# Demonstrated how to handle exceptions differently.
# This should appear.
# Demonstrated how finally is run after exceptions.
# Demonstrated how else is run when no exceptions occur.
# Demonstrated how to use a basic custom exception: Custom Exception.
try:
raiseException
except:
print"Basic exception raised."
try:
raiseException("ERROR")
exceptExceptionase:
print"Basic exception with message: %s."%str(e)
try:
importbadimport
exceptImportErrorase:
print"Caught non basic exception: %s."%str(e)
try:
test=1+'1'
exceptTypeErrorase:
print"Caught non basic exception: %s."%str(e)
try:
importbadimport
exceptImportError, TypeError:
print"Demonstrated how to handle multiple types of exceptions the same way."
try:
importbadimport
except (ImportError, TypeError) ase:
print"Demonstrated how to handle multiple types of exceptions the same way with a message: %s."%str(e)
try:
importbadimport
exceptImportError:
print"Demonstrated how to handle exceptions differently."
exceptTypeError:
print"This should not appear."
try:
importbadimport
exceptImportError:
print"This should appear."
finally:
print"Demonstrated how finally is run after exceptions."
try:
test=1+1
except:
print"This should not appear."
else:
print"Demonstrated how else is run when no exceptions occur."
classCustomBasicError(Exception):
""" Custom Exception Type - Can be customised further """
pass
try:
raiseCustomBasicError("Custom Exception")
exceptCustomBasicErrorase:
print"Demonstrated how to use a basic custom exception: %s."%str(e)