>>> s = input('Enter something --> ') # before you entering something, press Ctrl + D
Enter something -->
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
s = input('Enter something --> ')
EOFError: EOF when reading a line
>>>
看下面這段程序:
#!/usr/bin/env python
# Filename: try_except.py
import sys
import os
try:
s = input('Enter something --> ') # before you entering something,press Ctrl+D
except EOFError:
print(os.linesep + 'Why did you do an EOF on me?')
sys.exit() # exit the program
# here, we are not exiting the program
print('Well done.')
執行結果1:
>>> ================================ RESTART ================================
>>>
Enter something -->
Why did you do an EOF on me?
Traceback (most recent call last):
File "E:/myd/work/Python/try_except.py", line 9, in <module>
sys.exit() # exit the program
SystemExit
>>>
執行結果2:>>> ================================ RESTART ================================
>>>
Enter something --> Python is exceptional!
Well done.
raise語句:
#!/usr/bin/env python
# Filename: raising.py
class ShortInputException(Exception):
'''A user-defined exception class.'''
def __init__(self, length, atleast):
Exception.__init__(self)
self.length = length
self.atleast = atleast
try:
s = input('Enter something --> ')
if len(s) < 3:
raise ShortInputException(len(s), 3)
# Other work can continue as usual here
except EOFError: # press Ctrl+D
print('\nWhy did you do an EOF on me?')
except ShortInputException as x:
print('ShortInputException: The input was of length %d, \
which was expecting at least %d' % (x.length, x.atleast))
else:
print('No exception was raised.')
執行結果:
>>> ================================ RESTART ================================
>>>
Enter something -->
Why did you do an EOF on me?
>>> ================================ RESTART ================================
>>>
Enter something -->
ShortInputException: The input was of length 0, which was expecting at least 3
>>> ================================ RESTART ================================
>>>
Enter something --> d
ShortInputException: The input was of length 1, which was expecting at least 3
>>> ================================ RESTART ================================
>>>
Enter something --> Python
No exception was raised.
>>>
finally語句
#!/usr/bin/env python
# Filename: finally.py
import os
import time
import sys
os.system('cd "E:\myd\work\Python"')
poem = '''
Programming is fun
When the work is done
if you wanna make your work also fun:
use Python!
'''
f = open('poem.txt', 'w') # open for writing
f.write(poem) # write text to file
f.close()
try:
f = open('poem.txt')
while True:
line = f.readline()
if len(line) == 0:
break
time.sleep(2) # here you can press Ctrl+C to raise keyboard interruption
sys.stdout.write(line)
finally:
f.close()
print('Cleaning up...closed the file')
輸出:
>>> ================================ RESTART ================================
>>>
Programming is fun
When the work is done
if you wanna make your work also fun:
use Python!
Cleaning up...closed the file
>>> ================================ RESTART ================================
>>>
Programming is fun
When the work is done
Cleaning up...closed the file
Traceback (most recent call last):
File "E:/myd/work/Python/finally.py", line 22, in <module>
time.sleep(2) # here you can press Ctrl+C to raise keyboard interruption
KeyboardInterrupt
>>>
呵呵