Exception Groups in Python
In this article, we will see how we can use the latest feature of Python 3.11, Exception Groups. To use ExceptionGroup you must be familiar with Exception Handling in Python. As the name itself suggests, it is a collection/group of different kinds of Exception. Without creating Multiple Exceptions we can group together different Exceptions which we can later fetch one by one whenever necessary, the order in which the Exceptions are stored in the Exception Group doesn’t matter while calling them.
NOTE: If the user doesn’t have Python 3.11 installed on their device then this will not work.
Syntax of ExceptionGroup
Syntax: ExceptionGroup(“<User_Message/Description>”,[<SubException1(“exception message”)>,
<SubException2(“exception message”)>,…. <SubExceptionN(“exception message”)>])
Parameter: ExceptionGroup takes two parameters.
- First_Parameter – A message/description of what kind of Exceptions it will be storing or anything user wants to write there as a message of the ExceptionGroup.
- Second_Parameter – Any amount of sub-exceptions user want to store with their respective messages.
As you can see, even though we have created a function exceptionGroup() we are not returning any value, rather than that we are raising the variable in which we are storing the ExceptionGroup, because as we know in case of exception handling we raise an Exception. Now let’s create an ExceptionGroup and see how it works.
Now, what happens if we call this Function?
Users will get a structured new kind of error message which clearly states the number of Sub Exceptions we have in the Exception Group with their respective messages.
Now enclose them in a Python try-except block
In the snippet, we are using the traditional method of try-except block and calling the entire Exception group in except block and printing all the Sub Exception messages we have inside it. It will yield the following output.
The above snippet will return the following output, it is just printing all the Sub Exceptions and their messages in a tuple form.
Handling a single Exception from the ExceptionGroup
There is another great way to handle Sub Exceptions one by one without handling them all together like the above traditional method. For that, there is a new syntax – except*, which is used specifically with ExceptionGroup.
except* <ExceptionName> as <any_alias>: print(<any_alias>.exception)
As we can see, all the Exceptions and their respective error messages are displayed. Now if we want to only print the messages then we have to write code as below:
In case we only handle a single exception but there is more than one unhandled exception we will get output like this below:
As we can see that Python will give us the name of the Unhandled exceptions and their messages in a structured manner.
Nested Exception Group
We can have an ExceptionGroup inside of an ExceptionGroup i.e Nested Exception group. We will get this amazing structured output which clearly tells us about our nested Exception Group alongside our real Exception Group.
Please Login to comment...