[PYTHON] Review of exceptions

1


l = [1, 2, 3]
i = 5

print('start')
try:
    l[0]
except IndexError as ex:
    print('There is no such index.{}'.format(ex))
except NameError as ex:
    print('Not defined.{}'.format(ex))
except Exception as ex:
    print('other: {}'.format(ex))
else:
    print("It was processed normally.")
finally:
    print("end")

Execution result of 1


start
It was processed normally.
end

No error occurs, so The else block and finally block were executed.

2


l = [1, 2, 3]
i = 5

print('start')
try:
    l[i]
except IndexError as ex:
    print('There is no such index.{}'.format(ex))
except NameError as ex:
    print('Not defined.{}'.format(ex))
except Exception as ex:
    print('other: {}'.format(ex))
else:
    print("It was processed normally.")
finally:
    print("end")

Execution result of 2


start
There is no such index. list index out of range
end

Even though the index is only up to 2 Specify index 5 and IndexError occurs. So The except IndexError as ex block and the finally block were executed.

3


l = [1, 2, 3]
i = 5

del l

print('start')
try:
    l[0]
except IndexError as ex:
    print('There is no such index.{}'.format(ex))
except NameError as ex:
    print('Not defined.{}'.format(ex))
except Exception as ex:
    print('other: {}'.format(ex))
else:
    print("It was processed normally.")
finally:
    print("end")

Execution result of 3


start
Not defined. name'l' is not defined
end

Since l is gone with del l NameError occurs. So The except NameError as ex block and the finally block were executed.

4


l = [1, 2, 3]
i = 5

print('start')
try:
    l + ()
except IndexError as ex:
    print('There is no such index.{}'.format(ex))
except NameError as ex:
    print('Not defined.{}'.format(ex))
except Exception as ex:
    print('other: {}'.format(ex))
else:
    print("It was processed normally.")
finally:
    print("end")

Execution result of 4


start
other: can only concatenate list (not "tuple") to list
end

Since the list and tuple are added, An error that is neither IndexError nor NameError occurs. So The except Exception as ex block and the finally block were executed.

Recommended Posts

Review of exceptions
Catch multiple types of exceptions
ABC123D Review of past questions
Review of the basics of Python (FizzBuzz)
AtCoder Beginner Contest 102 Review of past questions
AtCoder Beginner Contest 072 Review of past questions
AtCoder Beginner Contest 085 Review of past questions
AtCoder Beginner Contest 062 Review of past questions
AtCoder Beginner Contest 051 Review of past questions
AtCoder Beginner Contest 127 Review of past questions
AtCoder Beginner Contest 119 Review of past questions
AtCoder Beginner Contest 151 Review of past questions
AtCoder Beginner Contest 075 Review of past questions
[Linux] Review of frequently used basic commands 2
AtCoder Beginner Contest 110 Review of past questions
AtCoder Beginner Contest 117 Review of past questions
AtCoder Beginner Contest 070 Review of past questions
AtCoder Beginner Contest 105 Review of past questions
AtCoder Beginner Contest 112 Review of past questions
AtCoder Beginner Contest 076 Review of past questions
AtCoder Beginner Contest 089 Review of past questions
AtCoder Beginner Contest 079 Review of past questions
AtCoder Beginner Contest 056 Review of past questions
AtCoder Beginner Contest 087 Review of past questions
AtCoder Beginner Contest 067 Review of past questions
AtCoder Beginner Contest 046 Review of past questions
AtCoder Beginner Contest 123 Review of past questions
AtCoder Beginner Contest 049 Review of past questions
AtCoder Beginner Contest 078 Review of past questions
AtCoder Beginner Contest 081 Review of past questions
Review the concept and terminology of regression
AtCoder Beginner Contest 047 Review of past questions
AtCoder Beginner Contest 060 Review of past questions
AtCoder Beginner Contest 104 Review of past questions
AtCoder Beginner Contest 057 Review of past questions
AtCoder Beginner Contest 121 Review of past questions
AtCoder Beginner Contest 126 Review of past questions
AtCoder Beginner Contest 090 Review of past questions
AtCoder Beginner Contest 103 Review of past questions
AtCoder Beginner Contest 061 Review of past questions
AtCoder Beginner Contest 059 Review of past questions
AtCoder Beginner Contest 044 Review of past questions
AtCoder Beginner Contest 083 Review of past questions
AtCoder Beginner Contest 048 Review of past questions
AtCoder Beginner Contest 124 Review of past questions
AtCoder Beginner Contest 116 Review of past questions
AtCoder Beginner Contest 097 Review of past questions
AtCoder Beginner Contest 088 Review of past questions
AtCoder Beginner Contest 092 Review of past questions
AtCoder Beginner Contest 099 Review of past questions
AtCoder Beginner Contest 065 Review of past questions
AtCoder Beginner Contest 053 Review of past questions
AtCoder Beginner Contest 094 Review of past questions
AtCoder Beginner Contest 063 Review of past questions
AtCoder Beginner Contest 107 Review of past questions
AtCoder Beginner Contest 071 Review of past questions
AtCoder Beginner Contest 064 Review of past questions
AtCoder Beginner Contest 082 Review of past questions
AtCoder Beginner Contest 084 Review of past questions
[Linux] Review of frequently used basic commands
AtCoder Beginner Contest 068 Review of past questions