2016-03-26   python 

Python写経(エラーと例外)

参照

http://docs.python.jp/3/tutorial/errors.html

SyntaxError

for n in range(0, 10)
    print(n)
  File "test.py", line 1
    for n in range(0, 10)
                        ^
SyntaxError: invalid syntax

FileNotFoundError

with open("important.txt", "r") as f:
    print(f.readlines())
Traceback (most recent call last):
  File "test.py", line 1, in <module>
    with open("important.txt", "r") as f:
FileNotFoundError: [Errno 2] No such file or directory: 'important.txt'

raise, try, except

try:
    raise NameError('My Message')
except NameError:
    print('Raised')
    raise
Raised
Traceback (most recent call last):
  File "test.py", line 2, in <module>
    raise NameError('My Message')
NameError: My Message
 2016-03-26   python