October 24, 2024
Chicago 12, Melborne City, USA
python

python custom exception not being caught


I have created an exception like this

class MyException(Exception):
    """
    myexception
    """

and then the code is like :

 try:
        _, _, test = test_str.rpartition("/")
        return test
    except MyException:
        print(
            f'An error occurred while transforming')
    return test_str

When the code is executed with test_str = None, the exception is not caught. If I replace MyException with Exception, it works as expected. What’s the issue ?

If I replace MyException with Exception, it works as expected.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video