Question: Purpose: User Defined Exception class in Python Python has good support handling Error and Exception, It has a well - defined Exception hierarchy and language
Purpose: User Defined Exception class in Python Python has good support handling Error and Exception, It has a welldefined Exception hierarchy and languagelevel support to throw and catch Exception and deal with them. Python Programmers often deal with builtin exceptions from Python and several others which are already defined eg NullPointerException. Please remember the advice our textbook gives regarding Exception. You should try to reuse the Exception classes provided instead of creating new ones for a similar purpose. But there are cases when a userdefined, custom exception provides a better opportunity to deal with special cases. So remember, you can always create your Exception classes by extending from the class Exception or one of its subclasses. Also, note that RuntimeException and its subclasses are not checked by the compiler and need not be declared in the method's signature. Therefore, use them with care, as you will not be informed and may not be aware of the exceptions that may occur by using that method and therefore do not have the proper exception handling codes a bad software engineering practice. User Defined Exception in PYTHON Here is an example of how to create a userdefined custom Exception in Python. By using this you can handle different error conditions differently. It's one of the simplest examples to understand the need for the customized exception in Java. Here we have an Account class, which represents a bank account where you can deposit and withdraw money, but what will happen if you want to withdraw money that exceeds your bank balance? You will not be allowed, and this is where userdefined exception comes into the picture. We have created a custom exception called NotSufficientFundException to handle this scenario. This will help you to show a more meaningful message to the user and programmer. Account.py Description: The Account class should be declared as a class and should meet all the requirements listed below. Its purpose is to represent a Bank account that holds your initial balance of $ at creation and provides two means of modifying your balance: one by withdrawing money and another by depositing money; and a simple method called balance which returns the actual balance. Instance Variables: balance ;stores a $ at the time of creation of a bank account. points Constructor: Account Parameters: none; points Methods: def getBalanceself: returns the actual account balance return self.balance; pointsdef withdrawself amount: withdraws amount from balance if there are sufficient funds. if amount self.balance: raise NotSufficientFundExceptionCurrent balance is less than requested amount" self.balanceself.balanceamount pointsdef depositselfamount:deposits amount in balance if amount is not if amount : raise ValueErrorInvalid deposit amount s amount; self.balanceself.balance amount points NotSufficientFundException.py Description: The NotSufficientFundException class should be declared as a class and should meet all the requirements listed below. Its purpose is to alert the user that heshe is withdrawing an amount of money that exceeds the amount that is available. Instance Variables: message;a String message to share with the user. points Constructor: NotSufficientFundException with parameter: message; points You need to implement that. def NotSufficientFundException self message: supercause; self.message message points Methods: def getMessage: return self.message; points TestDefinedException.py Description: The TestDefinedException class should be declared as a class and should meet all the requirements listed below. Its purpose is to create an object an Account object and test withdrawing money from the account twice to test the ideas proposed above. You should already be familiar with how to instantiate objects and print values to the screen using print Therefore, the actual implementation code for this assignment will not be provided. You may organize the output of data according to their specifications. However, you must perform the following tasks: Create an instance of Account, called acct. points Print the current balance of acct. points Next, we want to withdraw from acct. points Print the current balance of acct. points Next, we want to deposit from acct. points Print the current balance of acct. points Next, we want to withdraw $ from acct. points Print the current balance of acct. points Please make sure you follow points through without any additions or any more coding other than what is specified. Possible Sample Output: Your balance is: Your balance is: Your balance is: Traceback most recent call last:
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
