Catch multiple exceptions in one line (except block)
In Python, you can catch multiple exceptions in a single except block by separating the exceptions with a tuple. For example:
try: # some code here except (Exception1, Exception2, Exception3): # handle the exceptions
This will catch any of the specified exceptions (Exception1, Exception2, or Exception3) that are raised during the execution of the try block.
You can also give the except block a variable name to store the exception object that was raised. This can be useful if you want to access the attributes of the exception object (such as the error message) in your except block:
try: # some code here except (Exception1, Exception2, Exception3) as e: # handle the exception print(e) # print the exception object
Note that it is generally considered a best practice to catch specific exceptions rather than catching a broad, generic exception like Exception. This allows your code to handle the specific error conditions more gracefully and provide more informative error messages to the user.