Question

public void withdraw(double amount) throws InsufficientFundsException{ if (amount > this.balance) throw new InsufficientFundsException("Sorry, you have insufficient...

public void withdraw(double amount) throws InsufficientFundsException{
if (amount > this.balance) throw new InsufficientFundsException("Sorry, you have insufficient funds!");
       else {
this.balance = this.balance - amount;
}
}
}

can that be put in try-catch blocks? if so how does it look

0 0
Add a comment Improve this question Transcribed image text
Answer #1

It can be put in try and catch block since it may throw InsufficientFundsException. InsufficientFundsException is a user defined exception so it should be defined in which it extends Exception class. An example of how it will look is given below:

try{

withdraw(1000);

}

catch(InsufficientFundsException e)

{

System.out.println(e);

}

Add a comment
Know the answer?
Add Answer to:
public void withdraw(double amount) throws InsufficientFundsException{ if (amount > this.balance) throw new InsufficientFundsException("Sorry, you have insufficient...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • WHERE in this code should you put the main method so that the code will compile...

    WHERE in this code should you put the main method so that the code will compile WITHOUT changing anything else in the code? Simply adding the main method only? public class Account { private String name; private double balance;    public Account(String name, double balance) { this.name = name; if (balance > 0.0) this.balance = balance; } public void deposit(double depositAmount) { if (depositAmount > 0.0) balance += depositAmount; }    public double getBalance() { return balance; } public String...

  • If I enter a negative value the program throws an error and the withdrawal amount is...

    If I enter a negative value the program throws an error and the withdrawal amount is added to the balance please help me find why? public abstract class BankAccount { private double balance; private int numOfDeposits; private int numOfwithdrawals; private double apr; private double serviceCharge; //Create getter and setter methods public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public int getNumOfDeposits() { return numOfDeposits; } public void setNumOfDeposits(int numOfDeposits) { this.numOfDeposits =...

  • Use the Java implementation of transaction() public void transaction(Account from, Account to, double amount) { mutex...

    Use the Java implementation of transaction() public void transaction(Account from, Account to, double amount) { mutex lock1, lock2; lock1 = get_lock(from); lock2 = get_lock(to); acquire(lock1); acquire(lock2); withdraw(from, amount); deposit(to, amount); release(lock2); release(lock1); } The code does not need to have "Driver" (main method) to execute. Also use "Synchronized() method in Java" which provides exclusive access to an object (i.e., from Account or to Account in the transaction). Could you write the code please? Example of Synchronized() method (not my code):...

  • Can you plz help me this in java? QUESTION 14 Given public static void test() throws...

    Can you plz help me this in java? QUESTION 14 Given public static void test() throws FileNotFoundException{ try { throw FileNotFoundException(); } finally { Determine why it will not compile. Which statement(s) is(are) correct? (Choose all that apply) A. The code will not compile without a catch clause UB. The finally clause should be the final clause UC There is no class called FileNotFoundException D. None of these

  • How would I alter this code to have the output to show the exceptions for not...

    How would I alter this code to have the output to show the exceptions for not just the negative starting balance and negative interest rate but a negative deposit as well? Here is the class code for BankAccount: /** * This class simulates a bank account. */ public class BankAccount { private double balance; // Account balance private double interestRate; // Interest rate private double interest; // Interest earned /** * The constructor initializes the balance * and interestRate fields...

  • Please help me answer these questions in java programming language. Please answer it line after line...

    Please help me answer these questions in java programming language. Please answer it line after line so that I can understand please. Does it compile or not? No programming is done in this exercise, simply answer the questions asked in the code given below. class Exemple { /*Explain why this code does not compile */ public void m1() { foo(); } public int foo() throws Exception { throw new Exception(); } /*Explain why this code is not considered as good...

  • import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {              ...

    import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {               int total = 0;               int num = 0;               File myFile = null;               Scanner inputFile = null;               myFile = new File("inFile.txt");               inputFile = new Scanner(myFile);               while (inputFile.hasNext()) {                      num = inputFile.nextInt();                      total += num;               }               System.out.println("The total value is " + total);        } } /* In the first program, the Scanner may throw an...

  • package Lab09; public class Lab09Driver { public static void main(String[] args) { new Lab09Driver(); } public...

    package Lab09; public class Lab09Driver { public static void main(String[] args) { new Lab09Driver(); } public Lab09Driver() { Scanner input = new Scanner(System.in); System.out.println("Would you like to load a previous file? (Y/N)"); String.choice = input.nextLine().trim().toUpperCase(); if (choice.charAt(0) == 'Y') { System.out.println("Enter the name of the file that you want to load:"); String.fileName = input.nextLine(); try { Scanner fileInput = new Scanner(new File(fileName)); while(fileInput.hasNextLine()){ String answer = fileInput.nextLine(); System.out.println(answer); }fileInput.close(); } catch (FileNotFoundException e) { System.out.println("Could not find the file, and...

  • Lab 3 Step One First, create an empty directory for lab3. There is no starter code for this lab. You will be throwing an...

    Lab 3 Step One First, create an empty directory for lab3. There is no starter code for this lab. You will be throwing and catching exceptions in this exercise. Create a file called RuntimeException.h and put the following code in it. #include <string> class RuntimeException { private: string errorMsg; public: RuntimeException(const string& err) { errorMsg = err; } string getMessage() const { return errorMsg; } } Step Two In a new .cpp file in your directory, write a main function...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT