import sys, traceback
number1 = 100
number2 = 1
print('start')
try:
print(1)
raise NameError('意図的にネームエラーを発生させました。')
print(2)
except NameError as e:
print('未定義の変数を呼び出しました。')
sys.stderr.write(traceback.format_exc())
finally:
print('end')
実行結果
start
1
未定義の変数を呼び出しました。
end
tryブロックのraise NameError より後ろにある
print(2)は実行されないことに注意。
実行時エラー
Traceback (most recent call last):
File "Main.py", line 9, in <module>
raise NameError('意図的にネームエラーを発生させました。')
NameError: 意図的にネームエラーを発生させました。