Python:构造可容错的应用程序

来源:互联网 发布:tophat算法 编辑:程序博客网 时间:2024/06/05 22:55

1.新建myindexerror模块,命名为myindexerror.py

class MyIndexError:    def __init__ (self,value):        self.value=value

2.新建myvalueerror模块,命名为myvalueerror.py

class MyValueError:    def __init__ (self,value):        self.value=value

3.新建myerror.py文件

import myindexerrorimport myvalueerroruserList=['maxianglin','wanglili','malingling','fanxiaoxuan']user_str=''user_name=''input_selectIndex=0user_name=0try:    input_selectIndex=int(raw_input('请输入要查询的用户名编号:'))    user_str=userList[input_selectIndex]    input_selectName=raw_input('请输入要查询的用户名:')    user_name=userList.index(input_selectName)except IndexError,e:    print '出现的错误信息编号为:',myindexerror.MyIndexError('1').valueexcept ValueError,e:    print '出现的错误信息编号为:',myvalueerror.MyValueError('2').valueelse:    print '您输入的编号为'+str(input_selectIndex)+'的用户为:'+user_str    print '您输入的用户名'+input_selectName+'在列表中对应的索引为:'+str(user_name)
4.运行结果:

Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win32Type "copyright", "credits" or "license()" for more information.>>> ================================ RESTART ================================>>> 请输入要查询的用户名编号:4出现的错误信息编号为: 1>>> ================================ RESTART ================================>>> 请输入要查询的用户名编号:2请输入要查询的用户名:yinguopeng出现的错误信息编号为: 2>>> ================================ RESTART ================================>>> 请输入要查询的用户名编号:2请输入要查询的用户名:maxianglin您输入的编号为2的用户为:malingling您输入的用户名maxianglin在列表中对应的索引为:0>>> 

0 0
原创粉丝点击