Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

USER DEFINED EXCEPTION IN JAVA Here is an example of how to create a user-defined custom Exception in Java. By using this you can handle

USER DEFINED EXCEPTION IN JAVA Here is an example of how to create a user-defined custom Exception in Java. By using this you can handle different error condition 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 is representing a bank account where you can deposit and withdraw money, but what will happen if you want to withdraw money which 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 the more meaningful message to user and programmer. ACCOUNT.

JAVA Description: The Account class should be declared as a public class and should meet all the requirements listed below. Account() 1- Parameters: none; (7.5 points) METHODS: 1-public int balance() {//returns the actual account balance return balance;} (5 points) 2-public void withdraw(int amount) throws NotSufficientFundException 1 /withdraws amount from balance if there are sufficient funds. if (amount > balance) { throw new NotSufficientFundException(String.format(\"Current balance %d is less than requested amount %d\", balance, amount));} balance = balance - amount;}} (12.5 points) 3-public void deposit(int amount) { //deposits amount in balance if amount is not 0.>=>

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions

Question

T F Most data gathered for an MIS come from external sources.

Answered: 1 week ago