Advanced Exceptions
In Python, program execution errors are called Exception(s). If the Exceptions are not explicitly handled, they will halt program execution.
Python provides four keywords to manage Exceptions:
The finally clause is optional and can be omitted.
try:
statement 1
statement 2
statement 3
...
...
statement N
except <ExceptionName> as <identifier>:
statement 1
statement 1
...
statement N
finally:
statement 1
statement 2
statement ...
statement N
The <ExceptionName> is optional and can be omitted.
The as <identifier> clause is optional and can be omitted.
Now let's discuss the different components of Python's exception handling mechanism.
This block will contain the code that has the possibility of generating an exception.
This block will handle the appropriate Exception. It can include the exception name to address the respective Exception differently.
For example, if the code in the try block can generate ZeroDivisionError and can also cause IndexError. The appropriate exception block will catch it. Below is an example.
Enter a number:
8
Enter another number:
4
3
This block gets executed irrespective of whether the Exception was generated or not. It is optional. The finally block can be handy for closing resources such as File Handles or Network Connections.
The raise statement generates an exception. Below is the syntax:
Enter a number:
34
This is not the required number.
Bye
Python allows for managing multiple exceptions. It is advantageous as a program can generate different exceptions and thus should be handled differently. For example, ZeroDivisionError should be handled differently than IndexError.
Example:
Enter a number:
5
Enter another number:
4
n/m is 1.25
n*m is 20
During the development of large programs, there are numerous instances where a bug might seep through and cause incorrect processing leading to malfunctioning software for the end-user, leading to loss of opportunity, reputation, finance, etc.
One of the notable examples is the Y2K problem. You can read more about that here.
Exception handling solves such instances by catching such errors as they occur and halt any further execution unless explicitly handled.
Python provides several in-built exceptions. The most common are:
You can go through the entire list of exceptions in Python here.
Although there are several in-built Exceptions in Python, they might not be suitable for every scenario. To handle such instances, Python provides the ability to raise custom Exceptions with messages. Although we can implement our custom Exception, I won't describe the process here because it requires knowledge of Classes, an uncovered topic. However, if you are curious, you can refer to this article.
To send a Custom message in an Exception, Python provides the raise keyword.
Here is an example.
Guess the magic number:
6
This is not the required number.
Bye
Guess the magic number:
4
This is not the required number.
Bye
Guess the magic number:
100
This is not the required number.
Bye
Guess the magic number:
1000
Voila! You found the magic number. It's 1000.
Bye
In this chapter, we learned about Exceptions in Python and their importance and usage. We also learned the syntax of managing exceptions, explained Exception's different components, handled raised Exceptions, managed multiple Exceptions.
We also learned several advantages of using Exceptions to handle unpredictable error conditions, explained few in-built exceptions, and raised Exceptions with business-related(custom) messages for conciseness.