Rahul Mishra
Rahul Mishra

Reputation: 31

Detecting Exceptions in Parent class

How to know if an exception has occured in a Parent class method while overriding the same method in the child class ?

class Customer:
       def withdraw(self, amount):
             if self.__account_balance < self.get_min_balance:
                     raise LimitException()


class PrivCustomer(Customer):
       def withdraw(self, amount):
             """ here how do I detect if an exception has occurred in the withdraw method of Customer class ?""""

Upvotes: 1

Views: 177

Answers (1)

Patrick Haugh
Patrick Haugh

Reputation: 60944

Your PrivCustomer.withdraw method will replace the Customer.withdraw method for PrivCustomer objects. To call Customer.withdraw, you have to use super().withdraw to get the inherited method. That method call will be where LimitException is raised

class PrivCustomer(Customer):
    def withdraw(self, amount):
        try:
            super().withdraw(amount)
        except LimitException:
            ...

Upvotes: 4

Related Questions