Handling multiple exceptions
Example 1
One can handle different exceptions by using a single block of code, which can be grouped in a tuple, as shown in the example below.
Code:
try:
client_obj.get_url(url)
except (URLError, ValueError, SocketTimeout):
client_obj.remove_url(url)
Explanation:
The remove_url() method will have to be called if any of the listed exceptions occur.
Example 2
On the other hand, if any exceptions have to be handled separately, then the following code could help us.
Code:
try:
client_obj.get_url(url)
except (URLError, ValueError):
client_obj.remove_url(url)
except SocketTimeout:
client_obj.handle_url_timeout(url)
Explanation:
Here we separated the two exceptions, and thus, they can be handled separately now.
Example 3
Now instead of putting exceptions grouped into an inheritance hierarchy, all the exceptions can be caught simply by specifying a base class, like in the code given below.
Code:
try:
f = open(filename)
except (FileNotFoundError, PermissionError):
Explanation:
Here in the above code, the except statement follows a hierarchy, and we can omit this by using the given code below.
Code:
try:
f = open(filename)
except OSError:
Explanation:
The OSError mentioned above is a base class that is common to both the FileNotFoundError and also the PermissionError exceptions. Hence, it can be used to replace the hierarchy easily.
Example 4
It is unnecessary to handle multiple exceptions, and hence one can get a handle on the thrown exception using them as a keyword as used in the given code.
Code:
try:
f = open(filename)
except OSError as e:
if e.errno == errno.ENOENT:
logger.error('File not found')
elif e.errno == errno.EACCES:
logger.error('Permission denied')
else:
logger.error('Unexpected error: % d', e.errno)
Output:
File not found
Explanation:
The ‘e’ variable used above basically holds an instance of the raised OSError. This can prove useful if the exception has to be invested even further, maybe to process it based on the value of the additional status code.The clauses of the except are checked in the order list, and the first match is executed.
You can try it on online python compiler.
Also Read, leap year program in python
FAQs
Why do we say that exceptions occur at runtime only?
Exceptions are encountered while the program is running and are caused due to run time errors and not the errors due to syntax mistakes or compile time errors, and that’s why they occur only at runtime.
What is the difference between exception and syntax errors?
Syntax errors are identified by the compiler before the program is even executed, while exceptions are identified only when the program which is free of any syntax errors is being executed, and there some error is encountered.
Conclusion
This article extensively discussed the handling of exceptions in python. Exception handling is essential such that whenever any wrong user input or any other related issue is raised, the program can handle it well. We hope that this blog has helped you enhance your knowledge regarding Abstract Lists and if you would like to learn more, check out our articles on Coding Ninjas.
Do upvote our blog to help other ninjas grow. Happy Coding!