���� Python �̳�
��13�� �쳣
��һҳ try..except ��һҳ

try..except

����������ȡ�û���һ�����롣��Ctrl-d����һ�»ᷢ��ʲô��

>>> s = raw_input('Enter something --> ')
Enter something --> Traceback (most recent call last):
  File "<stdin>", line 1, in ?
EOFError

Python������һ����ΪEOFError�Ĵ�����������������ζ��������һ���������� �ļ�β ����Ctrl-d��ʾ��

�����������ǽ�ѧϰ��δ��������Ĵ���

�����쳣

���ǿ���ʹ��try..except����������쳣�����ǰ�ͨ����������try-���У��������ǵĴ�����������except-���С�

��13.1 �����쳣

#!/usr/bin/python
# Filename: try_except.py


import sys

try:
    s = raw_input('Enter something --> ')
except EOFError:
    print '\nWhy did you do an EOF on me?'
    sys.exit() # exit the program
except:
    print '\nSome error/exception occurred.'
    # here, we are not exiting the program

print 'Done'

��Դ�ļ���code/try_except.py��

���

$ python try_except.py
Enter something -->
Why did you do an EOF on me?

$ python try_except.py
Enter something --> Python is exceptional!
Done

������

���ǰ����п������������������try���У�Ȼ����except�Ӿ�/���д������еĴ�����쳣��except�Ӿ����ר�Ŵ�����һ�Ĵ�����쳣������һ�������Բ�����ڵĴ���/�쳣�����û�и���������쳣�����ƣ����ᴦ�� ���е� ������쳣������ÿ��try�Ӿ䣬���ٶ���һ���������except�Ӿ䡣

���ij��������쳣û�б�������Ĭ�ϵ�Python�������ͻᱻ���á�������ֹ��������У����Ҵ�ӡһ����Ϣ�������Ѿ������������Ĵ�����

�㻹������try..catch�������һ��else�Ӿ䡣��û���쳣������ʱ��else�Ӿ佫��ִ�С�

���ǻ����Եõ��쳣���󣬴Ӷ���ȡ�����и�����쳣����Ϣ���������һ��������˵����


��һҳ ��һ�� ��һҳ
���� ��ҳ �����쳣