python 异常-实例

1

try:
  name = ['a','b','c']
  name[3]
  info_dic = {}
  info_dic['alex']
#except (IndexError,KeyError):
#  print 'Your list is out of range...'

except IndexError:
  print 'Your list is out of range...'
except KeyError:
  print 'No valid key'


2.

[root@node110 error]# cat manual_error_class.py
#!/usr/bin/env python
class AlexException(Exception):
  def __init__(self,err):
    print 'The name you input is not correct!',err
try:
  name = raw_input('Name:').strip()
  if name != 'alex':
    raise AlexException(name)
except AlexException:
print 'No valid name sepecfied...'


3.

 [root@node110 error]# cat try_except_else.py
#!/usr/bin/env python
try:
  name = raw_input('Name:').strip()
  if len(name) == 0:
    raise KeyError
except:
  print 'No valid key'


[root@node110 error]# python try_except_else.py
Name:
No valid key
[root@node110 error]# python try_except_else.py
Name:rsc
No err found...


分割线
感谢打赏
江西数库信息技术有限公司
YWSOS.COM 平台代运维解决方案
 评论
 发表评论
姓   名:

Powered by AKCMS