The __str__ method in exceptions

Report a typo

We have created the exception class called NotInBoundsError:

class NotInBoundsError(Exception):
    def __str__(self):
        return "There is an error!"

It prints an error message. The class is already defined in the code, so you can use it.

First, create the function check_integer(num) that checks if the given integer lies between 45 and 67. If it does, the function should return the integer. If not, the NotInBoundsError should be raised.

Second, create a new function error_handling(num). It should call the check_integer(num) function and use the try-except block to catch the NotInBoundsError. If there is an error, print its error message. If there are no errors, print the result of the check_integer(num) function.

Sample Input 1:

8

Sample Output 1:

There is an error!
Write a program in Python 3
def check_integer(num):
...

def error_handling(num):
...
___

Create a free account to access the full topic