-
Notifications
You must be signed in to change notification settings - Fork 6
/
pyspaceinvaders_exception.py
49 lines (41 loc) · 1.74 KB
/
pyspaceinvaders_exception.py
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
# $LastChangedDate: 2009-08-19 15:06:10 -0500 (Wed, 19 Aug 2009) $
# Exception handling. Exception class Oops.
# Author: Jim Brooks http://www.jimbrooks.org
# Date: initial 2004/08, rewritten 2009/08
# License: GNU General Public License Version 2 (GPL2).
#===============================================================================
import traceback
#-------------------------------------------------------------------------------
# Exception handling. Exception class Oops.
#-------------------------------------------------------------------------------
def PrintCallStack():
""" Print call stack. """
traceback.print_exc( file=sys.stderr )
def PrintException( exception=None ):
""" Print information when an exception occurs. """
""" Historically, PrintException() with no arg was placed in an "except:" clause. """
""" However, the recommended new usage is to pass an exception object. """
if ( exception == None ):
print( "EXCEPTION: " )
else:
try:
print(( "EXCEPTION: \"" + str(exception) + "\"" ))
except:
print( "EXCEPTION: " )
PrintCallStack()
class Oops(Exception): # class Oops(object) would be incorrect
""" User-defined exception class. """
""" """
""" Example: """
""" try: """
""" raise Oops( "file missing" ) """
""" except Exception as exception: # class as instance """
""" PrintException( exception ) """
""" """
""" Note: 'except class as instance' is the new Python 2.6/3.x syntax. """
def __init__( self, text="" ):
self.text = text[:]
def __repr__( self ):
return self.text
def __str__( self ): # need both __repr__ and __str__ despite Python docs
return self.text