Saturday, November 16, 2024
Google search engine
HomeLanguagesPython | Assertion Error

Python | Assertion Error

Assertion ErrorĀ 
Assertion is a programming concept used while writing a code where the user declares a condition to be true using assert statement prior to running the module. If the condition is True, the control simply moves to the next line of code. In case if it is False the program stops running and returns AssertionError Exception.Ā 

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
assert y != 0, "Invalid Operation" # denominator can't be 0
print(x / y)


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 Operation

The 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




# Handling it manually
try:
Ā Ā Ā Ā x = 1
Ā Ā Ā Ā y = 0
Ā Ā Ā Ā assert y != 0, "Invalid Operation"
Ā Ā Ā Ā print(x / y)
Ā 
# the errror_message provided by the user gets printed
except AssertionError as msg:
Ā Ā Ā Ā print(msg)


Output :Ā 

Invalid Operation

Practical applications.Ā 
Example 3: Testing a program.Ā 

Python3




# Roots of a quadratic equation
import math
def ShridharAcharya(a, b, c):
Ā Ā Ā Ā try:
Ā Ā Ā Ā Ā Ā Ā Ā assert a != 0, "Not a quadratic equation as coefficient of x ^ 2 can't be 0"
Ā Ā Ā Ā Ā Ā Ā Ā D = (b * b - 4 * a*c)
Ā Ā Ā Ā Ā Ā Ā Ā assert D>= 0, "Roots are imaginary"
Ā Ā Ā Ā Ā Ā Ā Ā r1 = (-b + math.sqrt(D))/(2 * a)
Ā Ā Ā Ā Ā Ā Ā Ā r2 = (-b - math.sqrt(D))/(2 * a)
Ā Ā Ā Ā Ā Ā Ā Ā print("Roots of the quadratic equation are :", r1, "", r2)
Ā Ā Ā Ā except AssertionError as msg:
Ā Ā Ā Ā Ā Ā Ā Ā print(msg)
ShridharAcharya(-1, 5, -6)
ShridharAcharya(1, 1, 6)
ShridharAcharya(2, 12, 18)


Output :Ā 

Roots of the quadratic equation are : 2.0  3.0
Roots are imaginary
Roots of the quadratic equation are : -3.0  -3.0

This is an example to show how this exception halts the execution of the program as soon as the assert condition is False.Ā 

Other useful applications :Ā Ā 

  • Checking values of parameters.
  • Checking valid input/type.
  • Detecting abuse of an interface by another programmer.
  • Checking output of a function.

Ā 

Previous article
Next article
RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?