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...