Categories
Python Answers

How to use Python try…except as in except?

Spread the love

Sometimes, we want to use Python try…except as in except.

In this article, we’ll look at how to use Python try…except as in except.

How to use Python try…except as in except?

To use Python try…except as in except, we use as to assign the exception thrown to a variable.

For instance, we write

try:
    # ...
except (Exception1, Exception2) as e
    print('error')

to catch instances of Exception1 and Exception2 and assign the exception instance to e in the except block.

Conclusion

To use Python try…except as in except, we use as to assign the exception thrown to a variable.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *