How can I handle multiple same Error type in Python in try except block

Question:

I am trying to handle multiple same error in try except block. I have 2 function where I run 2nd function in except block if 1st block gives an error.

I tried raising the exception like many post suggested and it didn’t help. I am writing a simple code which in a way is similar running multiple function. How can i try 10/0 if it fails try 20/0 in except and if we get error go to last except block?

try:
    d =10/0
except ZeroDivisionError as e :

    d=20/0
except ZeroDivisionError as f:
    print("yes")

Result I am expecting according to my above code is “yes” since I get ZeroDivisionError twice.

Asked By: Sam

||

Answers:

You can chain try/except as follows.

try:
    d =10/0
except ZeroDivisionError as e :
    try:
        d=20/0
    except ZeroDivisionError as f:
        print("yes")
Answered By: Devesh Kumar Singh
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.