Menu
Coddy logo textTech

Exception Handling Example-3

Lesson 15 of 16 in Coddy's Exception Handling in Python course.

Exercise - 03

We have to create a function calculator which will handle all Custom & Built-in Exceptions.

It will take mathematical operation of string type (Expression) as argument. 

Below are Exception cases:-

1) Expression must contain two operands and an operator, separated by space. i.e 6 * 5 = 30. raise InvalidOperation user-defined Exception.

2) Validate operation symbol. Allowed operation symbols are (+, -, *, /). raise InvalidOperator user-defined Exception.

3) Operands must be numeric. raise Built-in TypeError.

4) Handle ZeroDivisionError. raise Built-in ZeroDivisionError.

challenge icon

Challenge

Easy

Solve above exercise. Print below messages as it is for Exceptions.

InvalidOperation :- Please enter two operands and operator separated by space.

InvalidOperator :- Invalid operator.

TypeError & ZeroDivisionError :- Standard Exception message.

Try it yourself

def calculator(exp):
    #your code goes here

All lessons in Exception Handling in Python