Python Menu

raise & assert statements in Python


raise statement (or) Raising exceptions

We can use raise to throw an exception if a condition occurs. i.e. If you want to throw an error when a certain condition occurs we can use raise keyword.

Syntax:
raise Exception_class

Example:
try:  
    age = int(input("Enter the age : "))  
    if age<18:  
        raise Exception;  
    else:  
        print("the age is valid")  
except Exception:  
    print("The age is not valid") 
Output: Case 1
Enter the age : 34
the age is valid
Output: Case 2
Enter the age : 12
The age is not valid
Example:
try:  
    a = int(input("Enter a : "))  
    b = int(input("Enter b : "))  
    if b is 0:  
        raise ArithmeticError;  
    else:  
        print("a/b = ",a/b) 
except ValueError:
	print("The values must be numbers")
except ArithmeticError:  
    print("The value of b can't be 0")
Output: Case 1
Enter a : 34
Enter b : 12
a/b =  2.8333333333333335
Output: Case 2
Enter a : 22
Enter b : 0
The value of b can't be 0
Output: Case 2
Enter a : 22
Enter b : sdf
The values must be numbers

assert statement (or) Assertions

Assertions are statements that assert or state a fact confidently in your program. For example, while writing a division function, you're confident the divisor shouldn't be zero, you assert divisor is not equal to zero.
Assertions are simply Boolean expressions that checks if the conditions return true or not. If it is true, the program does nothing and moves to the next line of code. However, if it's false, the program stops and throws an error.
It is also a debugging tool as it brings the program on halt as soon as any error is occurred and shows on which point of the program error has occurred.

assert Statement

Python has built-in assert statement to use assertion condition in the program. assert statement has a condition or expression which is supposed to be always true. If the condition is false assert halts the program and gives an AssertionError.

Syntax:
assert condition [, error_message]

In Python we can use assert statement in two ways as mentioned above.

  1. assert statement has a condition and if the condition is not satisfied the program will stop and give AssertionError.
  2. assert statement can also have a condition and a optional error message. If the condition is not satisfied assert stops the program and gives AssertionError along with the error message.
Example:
def avg(marks):  
    assert len(marks) != 0,"The List is empty."  
    return sum(marks)/len(marks)  
marks1 = [67,59,86,75,92]
print("The Average of Marks1:",avg(marks1))
marks2 = []
print("The Average of Marks2:",avg(marks2))
Output:
The Average of Marks1: 75.8
AssertionError: The List is empty.

In the above example, we have passed a non-empty list marks1 and an empty list marks2 to the avg() function. We received an output for marks1 list successfully, but after that, we got an error AssertionError: List is empty.

The assert condition is satisfied by the marks1 list and lets the program to continue to run. However, marks2 doesn't satisfy the condition and gives an AssertionError.

Example:
x = int(input("Enter x :"))  
y = int(input("Enter y :"))
# It uses assert to check for 0   
print ("x / y value is : ")   
assert y != 0, "Divide by 0 error" 
Output: Case 1
Enter x :33
Enter y :11
x / y value is :
3.0
Output: Case 2
Enter x :33
Enter y :0
x / y value is :
AssertionError: Divide by 0 error

Next Topic :Functions in Python