-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexception_catcher.py
More file actions
executable file
·93 lines (71 loc) · 3.04 KB
/
exception_catcher.py
File metadata and controls
executable file
·93 lines (71 loc) · 3.04 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
#!/usr/bin/env python
#
# exception_catcher.py - sample code for catching all exceptions except
# for the ones you specify
# Copyright (C) 2014 Michael Davies <michael@the-davies.net>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# Or try here: http://www.fsf.org/copyleft/gpl.html
#
# In Python...
# User-defined exceptions should be derived from Exception
# Built-in exceptions derive from StopIteration, StandardError, or Warning
# See https://docs.python.org/2/library/exceptions.html for more details
debug = False
class MyOwnException(Exception):
pass
class MoreSpecificException(MyOwnException):
pass
def is_standard_exception(ex):
"""Is the passed in exception considered a standard exception?
Determine whether the exception passed in should be caught
based upon whether it is a "standard" exception or not.
:param ex: The excetpion to check
:returns: True if the exception is a standard exception, False otherwise
"""
if (isinstance(ex, StopIteration) or
isinstance(ex, StandardError) or
isinstance(ex, Warning)):
if debug:
print ("%s is a standard exception" % type(ex).__name__)
return True
else:
if debug:
print ("%s is a NOT a standard exception" % type(ex).__name__)
return False
def check_raise_exception(ex):
"""Test raising exceptions to see if they are standard.
Raise the specified exception inside a local try/except block
and determine if it's a standard exception.
:param ex: The exception to raise and check
"""
try:
raise ex
except Exception as e:
return is_standard_exception(e)
if __name__ == '__main__':
import unittest
class TestExceptionHandling(unittest.TestCase):
def test_standard_exceptions(self):
self.assertTrue(check_raise_exception(StandardError))
self.assertTrue(check_raise_exception(StopIteration))
self.assertTrue(check_raise_exception(ImportError))
self.assertTrue(check_raise_exception(NotImplementedError))
self.assertTrue(check_raise_exception(Warning))
self.assertTrue(check_raise_exception(SyntaxWarning))
def test_local_exceptions(self):
self.assertFalse(check_raise_exception(MyOwnException))
self.assertFalse(check_raise_exception(MoreSpecificException))
unittest.main()