Python: Exception

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
try:
    # 処理
    linux_interaction()
except AssertionError as error:
    # 例外
    print(error)
else:
    ## 例外がない場合
    try:
        with open('file.log') as file:
            read_data = file.read()
    except FileNotFoundError as fnf_error:
        print(fnf_error)
finally:
    # 最後に必ず実行
    print('Cleaning up, irrespective of any exceptions.')