Sunday 8 December 2019

python - How to handle multiple exception in a try block?

Let's assume, a code consists of nearly 50 lines, and it has a try block. Consider a case that the try block has 10 unknown errors. Is it possible to handle those exceptions without specifying the name of the error in the except clause?



Here is the sample code:



try:
a = 2
b = 2 / 0

if 7 > 5:
print(7)
except(ZeroDivisionError, IndentationError)
print("Exception Handled")


In the above case, I know the name of the errors that occurred in the try block(say- ZeroDivisionError and IndentationError)
What if the name of the error is unknown?

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...