码迷,mamicode.com
首页 > 编程语言 > 详细

[Python]How to handle the exception in Python?

时间:2014-09-19 15:44:15      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:exception   python   

This post demonstrates how to use try clause to handle the exceptions

def test_exception(case=None):
	print case
	try:
		if case is None:
			raise ValueError("there is no value")
		elif case == 1:
			raise ImportError("can not import the module")
		else:
			raise MyException("this is the special error")
	except MyException as e:
		print e
		print "this is my exception"	
	except ValueError as e:
		print e
		print "this is value error"	
	except Exception as e:
		print "this is exception "

class MyException(Exception):
	pass

if __name__ == "__main__":
	#test_exception()
	test_exception(1)

 

[Python]How to handle the exception in Python?

标签:exception   python   

原文地址:http://blog.csdn.net/wonderfan/article/details/39397357

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!