| ???? Python ??? | ??13?? ?? | |
|---|---|---|
| ???? | 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-???С?
#!/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???
???????????????б??????????Python????????????á????????????????У??????????????????????????????????????
????????try..catch??????????else???????????????????else??佫????С?
????????????????????????????и??????????????????????????????????
| ???? | ????? | ???? |
|---|---|---|
| ???? | ??? | ?????? |