The function of assert statement is the same irrespective of the language in which it is implemented, it is a language-independent concept, only the syntax varies with the programming language.
Syntax of assertion:
assert condition, error_message(optional)
Example 1: Assertion error with error_message.
Python3
# AssertionError with error_message.
x= 1
y= 0
Invalid Operation0 Invalid Operation1= 0Invalid Operation4Invalid Operation5 Invalid Operation6
Invalid Operation7Invalid Operation8Invalid Operation9 Roots of the quadratic equation are : 2.0 3.0 Roots are imaginary Roots of the quadratic equation are : -3.0 -3.00
Output :
Traceback (most recent call last): File "/home/bafc2f900d9791144fbf59f477cd4059.py", line 4, in assert y!=0, "Invalid Operation" # denominator can't be 0 AssertionError: Invalid OperationThe default exception handler in python will print the error_message written by the programmer, or else will just handle the error without any message.
Both of the ways are valid.
Handling AssertionError exception:
AssertionError is inherited from Exception class, when this exception occurs and raises AssertionError there are two ways to handle, either the user handles it or the default exception handler.
In Example 1 we have seen how the default exception handler does the work.
Now let’s dig into handling it manually.
Example 2
Python3
Roots of the quadratic equation are : 2.0 3.0 Roots are imaginary Roots of the quadratic equation are : -3.0 -3.01
Roots of the quadratic equation are : 2.0 3.0 Roots are imaginary Roots of the quadratic equation are : -3.0 -3.02Roots of the quadratic equation are : 2.0 3.0 Roots are imaginary Roots of the quadratic equation are : -3.0 -3.03
Roots of the quadratic equation are : 2.0 3.0 Roots are imaginary Roots of the quadratic equation are : -3.0 -3.04x= 1
Roots of the quadratic equation are : 2.0 3.0 Roots are imaginary Roots of the quadratic equation are : -3.0 -3.04y= 0
Roots of the quadratic equation are : 2.0 3.0 Roots are imaginary Roots of the quadratic equation are : -3.0 -3.04Invalid Operation0 Invalid Operation1= 0Invalid Operation4Invalid Operation5
Roots of the quadratic equation are : 2.0 3.0 Roots are imaginary Roots of the quadratic equation are : -3.0 -3.04Invalid Operation7Invalid Operation8Invalid Operation9 Roots of the quadratic equation are : 2.0 3.0 Roots are imaginary Roots of the quadratic equation are : -3.0 -3.00
x4
x5 x6
Roots of the quadratic equation are : 2.0 3.0 Roots are imaginary Roots of the quadratic equation are : -3.0 -3.04Invalid Operation7x9
Output :
Invalid OperationPractical applications.
Example 3: Testing a program.
Python3
=0
=1 =2
=3 =4
Roots of the quadratic equation are : 2.0 3.0 Roots are imaginary Roots of the quadratic equation are : -3.0 -3.04Roots of the quadratic equation are : 2.0 3.0 Roots are imaginary Roots of the quadratic equation are : -3.0 -3.02Roots of the quadratic equation are : 2.0 3.0 Roots are imaginary Roots of the quadratic equation are : -3.0 -3.03
=8Invalid Operation0 10= 0Invalid Operation414
=816= 1819 y0y1 y2 19 y419y6
=8Invalid Operation0 y9= 0Invalid Operation4=3
=8=5= =7y1y000 01Invalid Operation9=704 19 06
=808= =7y1y0y1 01Invalid Operation9=704 19 06
=8Invalid Operation7=7Invalid Operation13Invalid Operation14
Roots of the quadratic equation are : 2.0 3.0 Roots are imaginary Roots of the quadratic equation are : -3.0 -3.04x5 x6
=8Invalid Operation7x9
Invalid Operation21y11Invalid Operation4Invalid Operation25Invalid Operation4y1Invalid Operation28Invalid Operation29
Invalid Operation211Invalid Operation41Invalid Operation4Invalid Operation28Invalid Operation29
Invalid Operation2104Invalid Operation4Invalid Operation40Invalid Operation4Invalid Operation42Invalid Operation29
Output :
Roots of the quadratic equation are : 2.0 3.0 Roots are imaginary Roots of the quadratic equation are : -3.0 -3.0This is an example to show how this exception halts the execution of the program as soon as the assert condition is False.