Fout die zou kunnen gebeuren afhandelen.
try:
f = open("foo")
except IOError:
print "Couldn’t open ’foo’. Sorry."
def factorial(n):
if n < 0:
raise ValueError,"Expected non-negative number"
if (n <= 1): return 1
else: return n*factorial(n-1)
Instantie doorgeven of standaard sys.exc_info afdrukken:
try:
parser.feed(self.content)
except sgmllib.SGMLParseError, inst:
self.error = inst
return 0
except UnicodeEncodeError, inst:
self.error = inst
return 0
except:
self.error = sys.exc_info()[0]
Uncaught exception
>>> factorial(-1)
Traceback (innermost last):
File "<stdin>", line 1, in ?
File "<stdin>", line 3, in factorial
ValueError: Expected non-negative number
>>>
Add A Comment
You must be logged in to post a comment.
