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
EasySolve 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 hereAll lessons in Exception Handling in Python
4Customization/best practices
Customize Tracebacks3User-defined Exceptions
Types of ExceptionsRaising an ExceptionUser Defined ExceptionException Handling Exercise- 1Exception Handling Example-2Exception Handling Example-3