Tutorial by Examples

Here, we have created a user-defined exception called CustomError which is derived from the Exception class. This new exception can be raised, like other exceptions, using the raise statement with an optional error message. class CustomError(Exception): pass x = 1 if x == 1: rais...
This example shows how to catch custom Exception class CustomError(Exception): pass try: raise CustomError('Can you catch me ?') except CustomError as e: print ('Catched CustomError :{}'.format(e)) except Exception as e: print ('Generic exception: {}'.format(e)) Output:...

Page 1 of 1