Given a piece of code that can throw any of several different exceptions, and one needs to account for all of the potential exceptions that could be raised without creating duplicate code or long, meandering code passages.
If you can handle different exceptions all using a single block of code, they can be grouped together in a tuple as shown in the code given below :
Code #1 :
remove_url() method will be called if any of the listed exceptions occurs. If, on the other hand, if one of the exceptions has to be handled differently, then put it into its own except clause as shown in the code given below :
Code #2 :
Many exceptions are grouped into an inheritance hierarchy. For such exceptions, all of the exceptions can be caught by simply specifying a base class. For example, instead of writing code as shown in the code given below –
Code #3 :
Except statement can be re-written as in the code given below. This works because
OSError is a base class that’s common to both the
FileNotFoundError and PermissionError exceptions.
Code #4 :
Although it’s not specific to handle multiple exceptions per se, it is worth noting that one can get a handle to the thrown exception using them as a keyword as shown in the code given below.
Code #5 :
The e variable holds an instance of the raised OSError. This is useful if the exception has to be invested further, such as processing it based on the value of the additional status code. The except clauses are checked in the order listed and the first match executes.
Code #6 : Create situations where multiple except clauses might match
Traceback (most recent call last): File "", line 1, in FileNotFoundError: [Errno 2] No such file or directory: 'miss
Here the except
FileNotFoundError clause doesn’t execute because the
OSError is more general, matches the FileNotFoundError exception, and was listed first.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python | Raising an Exception to Another Exception
- Python Exception Handling
- Handling TypeError Exception in Python
- Handling NameError Exception in Python
- Handling OSError exception in Python
- Handling a thread's exception in the caller thread in Python
- Handling EOFError Exception in Python
- Python | Reraise the Last Exception and Issue Warning
- Create an Exception Logging Decorator in Python
- How to pass argument to an Exception in Python?
- Test if a function throws an exception in Python
- How to print exception stack trace in Python?
- How to log a Python exception?
- How to print the Python Exception/Error Hierarchy?
- EnvironmentError Exception in Python
- How to handle a Python Exception in a List Comprehension?
- Precision Handling in Python
- ML | Handling Imbalanced Data with SMOTE and Near Miss Algorithm in Python
- Python | Handling recursion limit
- Python | Handling no element found in index()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.