Question: Assignment 5 : Test Defined Exception Purpose: User Defined Exception class in Python Python has good support handling Error and Exception, It has a well

Assignment 5: Test Defined Exception
Purpose:User Defined Exception class in Python
Python has good support handling Error and Exception, It has a well-defined Exception hierarchy and language-level support to throw and catch Exception and deal with them. Python Programmers often deal with built-in exceptions from Python and several others which are already defined e.g. 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 user-defined, 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 user-defined 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 user-defined 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 $1000 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 =1000;//stores a $1000 at the time of creation of a bank account. (2.5 points)
Constructor: Account()1- Parameters: none;(7.5 points)
Methods:
1-def getBalance(self):
//returns the actual account balance
return self.balance; (5 points)
2-def withdraw(self, amount):
//withdraws amount from balance if there are sufficient funds.
if (amount > self.balance):
raise NotSufficientFundException("Current balance is less than requested amount")
self.balance=self.balance-amount(12.5 points)
3-def deposit(self,amount)://deposits amount in balance if amount is not <0
if (amount <=0):
raise ValueError("Invalid deposit amount %s", amount));
self.balance=self.balance + amount(12.5 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 he/she is withdrawing an amount of money that exceeds the amount that is available.
Instance Variables:
message;//a String message to share with the user. (2.5 points)
Constructor: NotSufficientFundException() with 1 parameter: message; (7.5 points). You need to implement that.
def NotSufficientFundException (self, message):
super(cause);
self.message = message (10 points)
Methods:
1- def getMessage():
return self.message;(10 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.(5 points)
Print the current balance of acct.(2.5 points)
Next, we want to withdraw 400 from acct.(5 points)
Print the current balance of acct.(2.5 points)
Next, we want to deposit 300 from acct.(5 points)
Print the current balance of acct.(2.5 points)
Next, we want to withdraw $1000 from acct.(5 points)
Print the current balance of acct.(2.5 points)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!