forked from RoyGBivDash/errors
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththrow_err.py
More file actions
executable file
·82 lines (74 loc) · 1.93 KB
/
throw_err.py
File metadata and controls
executable file
·82 lines (74 loc) · 1.93 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
#!/usr/bin/env python
# This script raises an error based on
# user-supplied command line argument
import math
import sys
import os
import itertools
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("error_type")
args = parser.parse_args()
error_type = args.error_type
if error_type == "assertion":
f = 3
assert f== 4
elif error_type == "io":
f = open('myfile.txt')
elif error_type == "import":
import Config as CF
elif error_type == "index":
spam = ['cat', 'dog', 'mouse']
print(spam[6])
elif error_type == "key":
spam = {'cat': 'Kit', 'dog': 'Dawg'}
print('The name of my pet giraffe is' + spam['zebra'])
elif error_type == "name":
hoopla = 'Paul'
print('My name is ' + hoopl)
elif error_type == "os":
for i in range(10):
print i, os.ttyname(i)
elif error_type == "type":
x = '2' + 2
elif error_type == "value":
x = int("foo")
elif error_type == "zerodivision":
x = 1/0
elif error_type == "StopIteration":
l = [0, 1, 2]
i = iter(l)
print i
print i.next()
print i.next()
print i.next()
print i.next()
elif error_type == "GeneratorExit":
def my_generator():
for i in range(5):
print 'Yielding', i
yield i
g = my_generator()
print g.next()
g.close()
elif error_type == "keyboard":
try:
print 'Press Retuen or Ctrl-C:',
ignored = raw_input()
except Exception, err:
print 'Caught exception:', err
except KeyboardInterrupt, err:
print 'Caught KeyboardInterrupt'
else:
print 'No exception'
elif error_type == "syntax":
try:
print eval('five times three')
except SyntaxError, err:
print 'Syntax error %s (%s-%s): %s' % \
(err.filename, err.lineno, err.offset, err.text)
print err
else:
sys.stderr.write("Sorry, not able to throw a(n) ")
sys.stderr.write(error_type + " error\n")
print_usage()