Life is short, you need Python

自定义异常

异常通常直接或间接的从Exception类派生

#! /usr/bin/python

import sys,os

class MyError(Exception):
def __init__(self,value):
self.value
= value
def __str__(self):
return repr(self.value)

try:
f
= open('myfile.txt')

except IOError, (errno,strerror):
print "I/O error(%s): %s" %(errno,strerror)

#raise NameError, "Just for test! "
raise MyError, "my error is raised!"

 

the result:

Traceback (most recent call last):
File
"test.py", line 18, in <module>
raise MyError,
"my error is raised!"
__main__.MyError: 'my error is raised
!'

 

posted @ 2010-10-20 10:03  runfox545  阅读(179)  评论(0编辑  收藏  举报
白月黑羽 Python教程 白月黑羽Python