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
·96 lines (87 loc) · 2.36 KB
/
throw_err.py
File metadata and controls
executable file
·96 lines (87 loc) · 2.36 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
91
92
93
94
95
96
#!/usr/bin/env python
# This script raises an error based on
# user-supplied command line argument
import sys
import os
import argparse
parsified = argparse.ArgumentParser()
parsified.add_argument("error_type")
result = parsified.parse_args()
error_type = result.error_type
class newerror(Exception):
def __init__(self, arg):
self.msg = arg
#new error type, should return its arguments
#
#def print_usage():
# """Print usage and exit"""
# sys.stderr.write("usage: python raise_err.py <error type>\n")
# sys.stderr.write("available errors: \n")
# sys.stderr.write("\tassertion, io, import, index\n")
# sys.stderr.write("\tkey, name, os, type, value,\n")
# sys.stderr.write("\tzerodivision, newerror\n")
# sys.exit()
#
def contains(char_string, char):
# adjust returned index to account for searching in reverse
return len(char_string) - char_string[::-1].index(char) - 1
# Check args
# if len(sys.argv) != 2:
# print_usage()
#
# error_type = sys.argv[1]
if error_type == "assertion":
# raise AssertionError
# try:
assert 1==2
# except AssertionError:
print 'not likely'
# exit(1)
elif error_type == "io":
# raise IOError
file = open('nothere.txt')
print file
elif error_type == "import":
import os
os.chdir ("fail")
# from error import testest_foo.py
# raise ImportError
elif error_type == "index":
# dogs = {0: 'racer', 1: 'dixie', 2: 'heela'}
dogs =[1,2]
a = dogs[3]
# raise IndexError
elif error_type == "key":
array = {a: 'duck', b: 'duck'}
print array['c']
# raise KeyError
elif error_type == "name":
print what
# raise NameError
elif error_type == "os":
for i in range(9):
print n
# raise OSError
elif error_type == "type":
# raise TypeError
b = 'dog'
c = 'cat'
print (b / c)
elif error_type == "value":
# raise ValueError
contains ('atgcaggtacaba', 'k')
elif error_type == "zerodivision":
print 3.1415926/0
# raise ZeroDivisionError
elif error_type == "newerror":
# raise newerror("look - custom error type")
try:
# Raise an exception with argument
raise newerror('This is a new Error')
except newerror, arg:
# Catch the custom exception
print 'Error: ', arg.msg
else:
sys.stderr.write("Sorry, not able to throw a(n) ")
sys.stderr.write(error_type + " error\n")
print_usage()