Assertion Errors and Attribute Errors in Python
Assertion Error in Python
In Python, the assert condition is used to continue the execution if the given statement displays true. If the assert statement displays false, it raises Assertion Error exception with user defined error message. If the statement is true, the process will move to the next statement of code. If it fails, the program stops the execution and displays Assertion Error Exception.
Assertion Error is a language independent concept. Its syntax was different from programming language and when we are implementing the Assert condition, it ignores the same language.
Assertion syntax:
Assert condition, error message (optional)
Example
// python program to illustrate Assertion Error with error message
# Assertion Error with error message.
a =1
b =0
assertb !=0, "Invalid Operation"// denominator can't be 0
print (a /b)
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
As we known that when a denominator divides with 0, an error will be displayed. The exception handler in Python displays the user defined error message as written in code or directly it will display an error message.
Handling Assertion Error exception:
There are basically two ways to handle Assertion Errors:
- User handling
- Default exception handling
Let’s see an example for quadratic equation
// python program for quadratic equation
importmath
defquad (a, b, c):
try:
asserta !=0, "Not a quadratic equation as coefficient of x ^ 2 can't be 0"
D =(b *b -4*a*c)
assertD>=0, "Roots are imaginary"
r1 =(-b +math.sqrt(D))/(2*a)
r2 =(-b -math.sqrt(D))/(2*a)
print("Roots of quad are :", r1, "", r2)
exceptAssertionError as msg:
print(msg)
quad (-1, 5, -6)
quad (1, 1, 6)
quad (2, 12, 18)
Output
Roots of quad are: 2.0 3.0
Roots are imaginary
Roots of quad are: -3.0 -3.0
Attribute Errors in Python
In programming languages, when we execute programs, we face errors or exceptions in code. So, that program will not execute, or the output of the program will not be displayed. To rectify the errors, we debug the code.
Attribute Errors
Attribute Error is one of the errors which occur mostly in Python. Attribute Error can be defined as an error that is occurred when an assigned assignment or reference fails.
For example: if we take a variable y and assign a value of 5 and again if we assign another value to same variable, it is not possible.
Because, we have taken an integer type variable, it does not support the repeated values to the same variable, again and again. So, this type of problem is known as “Attribute Error”. To support the repeated value to a same variable we use list type. Then we don’t face the problem of getting “Attribute Error”.
Examples for getting an Attribute Error
Example 1
// python program to illustrate Attribute Error
J = 5
J. append (2) // raises an Attribute Error
Output:
Traceback (most recent call last):
File "/home/46576cfdd7cb1db75480a8653e2115cc.py", line 5, in
X.append (2)
AttributeError: 'int' object has no attribute 'append'
Example 2
//python program to illustrate Attribute Error
// An Attribute Error raises when method as fst for strings
String = “javatpoint contains”. fst (“good content”)
Print (String)
Output:
Traceback (most recent call last):
File "/home/2078367df38257e2ec3aead22841c153.py", line 3, in
string = "javatpoint contains".fst("good content")
AttributeError: 'str' object has no attribute 'fst'
Example 3
Attribute Error also occurs when user perform an incorrect Attribute reference for a user defined class.
// python program to illustrate Attribute Error.
classDemo ():
def__init__(self):
self.a ='javatpoint'
// Driver's code
obj =Demo()
print(obj.a)
// occurs an Attribute Error because we have not intialised b
Print(obj.b)
Output:
javatpoint
Error:
Traceback (most recent call last):
File "/home/373989a62f52a8b91cb2d3300f411083.py", line 17, in
Print(obj.b)
AttributeError: 'Demo' object has no attribute 'b'
Solution for Attribute Error
C programming does not provide direct support for error handling but in Python and Java programming exceptions and errors are handled by exception handling with the help of try and except in Python whereas in Java we use try and catch keywords.
Example
From above class example, we have to use exception handling for rectify or to cover come from Attribute Error.
// python program to illustrate Attribute Error
classDemo ():
def__init__(self):
self. a ='javatpoint'
obj =Demo ()
// Try and except keywords forException handling
try:
print(obj.a)
// Raises an AttributeError
Print(obj.b)
// Prints the below statement
// whenever an AttributeError israised
exceptAttributeError:
print("Attribute Error raised")
Output:
javatpoint
Attribute Error raised