You can use the raise method in Python to manually throw an exception that can be caught in the catch block as shown in the below example –
1 2 3 4 5 6 7 8 9 10 11 |
def myfun1(): try: raise ValueError('This is a manually created exception') raise Exception('This is the exception you expect to handle') except Exception as error: print('Caught this error: ' + repr(error)) myfun1() ## Returns ## Caught this error: ValueError('This is a manually created exception') |