a=[1,2,3]
try:
print(a[6])
except IndexError:
print(a[7])
except IndexError:
print(a[1])
Traceback (most recent call last):
File "", line 2, in
IndexError: list index out of range
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "", line 4, in
IndexError: list index out of range
so, I have to use a not graceful way:
a=[1,2,3]
try:
print(a[6])
except IndexError:
try:
print(a[7])
except IndexError:
print(a[1])
Is there any good approach to write the code?
exceptblocks on atryare used to catch multiple exception types that could happen in thetry, not exceptions happening inexceptblocks. - user2357112 supports Monicaexcept (IndexError, ValueError) as e- nico