How does Python catch an exception and continue to run?

try:
    1/0
    print(1)
except ZeroDivisionError:
    pass

as above, I want print (1) to continue to run after ZeroDivisionError is triggered.
it"s not realistic to write two try. What if I have 100 print behind me? It"s stupid to write 100 try.

means that there are several statements that are independent of each other, and they may trigger, for example, a ZeroDivisionError exception, so I don"t want the statement after the exception statement to stop running, but continue to run.

try:
    1/0
    print(1)
    1/0
    print(1)
    1/0
    print(1)
    1/0
    print(1)
    1/0
    print(1)
    1/0
    print(1)
    1/0
    print(1)
    1/0
    print(1)
    1/0
    print(1)
    1/0
    print(1)
    1/0
    print(1)
    1/0
    print(1)
except ZeroDivisionError:
    pass

for example, in the above paragraph, I want him to ignore all 1A zeros and print out all print (1)

.

how is it that when multiple lines of code are running, each line has different functions, but is independent of each other and not related to each other, and one of them is abnormal, ignoring the exception, and then the lines that are not running continue to run?

what cycle? exec,eval is written in stupid way. Is there any ingenious way to realize it? thank you

.
Jul.14,2022

you can extract these same divisions, write them as a function, and then reuse the function, passing in different parameters for each calculation.
similar to the following:

def division(x,y):
    try:
        z = x/y
        print(z)
    except ZeroDivisionError:
        pass
division(1,0)
division(2,0)
division(3,0)

try should be placed before statements that may make mistakes, and there should be as few statements as possible. Print (1) is impossible to make mistakes and should be placed outside the try..except.

try:
    1/0
except:
    pass
print(1)
...

try:
    xxx
except:
    pass
Menu