Note that there are some explanatory texts on larger screens.

plurals
  1. POThrow Exception only in one case
    primarykey
    data
    text
    <p>I have a assignment for my computer science class working with throwing exceptions. The assignment creates an applet to manage the bank account of a user.</p> <p>My problem is as follows: I need to throw an exception (InsufficientFundsException) only when a user tries to withdraw more money than is in the balance. However, I only want the InsufficientFundsException to be thrown in the case of a withdrawal, not a deposit. </p> <p>Here is the code for the methods that the problem is focused on MINUS the parts I am having problems with:</p> <p>The actionPerformed method (I am implementing the ActionListener interface)</p> <p>This method needs to, based on the button clicked -- the deposit or withdraw button -- process that transaction method. The transaction methods merely add or subtract the amount passed to the method to the Account object's balance. Then if there is an exception, it will catch the exception and print the error etc. HOWEVER the exceptions caught currently apply to both the withdraw and deposit transactions, while the one still needing to be thrown -- the InsufficientFundsException -- only applies to the withdraw transaction. Also, I cannot modify the withdraw method in the Account object to throw the exception.</p> <p>The getAmount method:</p> <p>I do not have the option to throw the InsufficientFundsException in this method</p> <p>I tried to add this:</p> <pre><code>if(getAmount(tf4) &gt; acc1.getBalance()) { throw InsufficientFundsException("Insufficient Funds"); } </code></pre> <p>right before the withdraw call here:</p> <pre><code> if (e.getSource() == withdraw) { try { acc1.withdraw(getAmount(tf4)); refreshFields(); status.setText("Withdrawal Processed"); } catch(EmptyFieldException ex) { status.setText(ex.getMessage() + "withdraw"); } catch(NumberFormatException ex) { status.setText(ex.getMessage() + "withdraw"); } catch(NegativeAmountException ex) { status.setText(ex.getMessage() + "withdraw"); } } </code></pre> <p>but I get an error because the actionPerformed method can't <code>throws</code> an InsufficientFundsException.</p> <p>Also if I try to <code>catch</code> an InsufficientFundsException in one try/catch block, I get an error for the other try/catch block since I haven't caught the exception... because I don't want to catch that exception.</p> <p>Here is the complete code, as requested:</p> <pre><code>import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*; import java.text.*; public class AccountApplet extends JApplet implements ActionListener { Container c = getContentPane(); JPanel p1 = new JPanel(), p2 = new JPanel(), p3 = new JPanel(), p4 = new JPanel(); JLabel lab1 = new JLabel("Account ID"); JLabel lab2 = new JLabel("Account Balance"); JLabel lab3 = new JLabel("Deposit"); JLabel lab4 = new JLabel("Withdraw"); JLabel status = new JLabel("Status Bar"); JTextField tf1 = new JTextField(), tf2 = new JTextField(), tf3 = new JTextField(), tf4 = new JTextField(); JButton deposit = new JButton("Deposit"), withdraw = new JButton("Withdraw"); Account acc1 = new Account(1234, 1000.00); double amount; public void init() { tf1.setEditable(false); tf2.setEditable(false); c.setLayout(new BorderLayout()); c.add(p1, BorderLayout.WEST); p1.setBorder(new TitledBorder("Display Account Information")); p1.setLayout(new GridLayout(2,2)); p1.add(lab1); p1.add(tf1); p1.add(lab2); p1.add(tf2); c.add(p2, BorderLayout.EAST); p2.setBorder(new TitledBorder("Deposit or Withdraw Funds")); p2.setLayout(new BorderLayout()); p2.add(p3, BorderLayout.WEST); p2.add(p4, BorderLayout.EAST); p3.setLayout(new GridLayout(2,1)); p3.add(lab3); p3.add(lab4); p4.setLayout(new GridLayout(2,2)); p4.add(tf3); p4.add(deposit); p4.add(tf4); p4.add(withdraw); c.add(status, BorderLayout.SOUTH); deposit.addActionListener(this); withdraw.addActionListener(this); refreshFields(); } public void actionPerformed(ActionEvent e) { status.setText(""); if (e.getSource() == deposit) { try { acc1.deposit(getAmount(tf3)); refreshFields(); status.setText("Withdrawal Processed"); } catch(EmptyFieldException ex) { status.setText(ex.getMessage() + "deposit"); } catch(NumberFormatException ex) { status.setText(ex.getMessage() + "deposit"); } catch(NegativeAmountException ex) { status.setText(ex.getMessage() + "deposit"); } } if (e.getSource() == withdraw) { try { acc1.withdraw(getAmount(tf4)); refreshFields(); status.setText("Withdrawal Processed"); } catch(EmptyFieldException ex) { status.setText(ex.getMessage() + "withdraw"); } catch(NumberFormatException ex) { status.setText(ex.getMessage() + "withdraw"); } catch(NegativeAmountException ex) { status.setText(ex.getMessage() + "withdraw"); } } } public void refreshFields() { tf1.setText(String.valueOf(acc1.getId())); tf2.setText("$" + String.valueOf(acc1.getBalance())); tf3.setText(""); tf4.setText(""); status.setText(""); } public double getAmount(JTextField tf) throws EmptyFieldException , NumberFormatException, NegativeAmountException { if(tf.getText().equals("")) { throw new EmptyFieldException("Empty field not allowed for "); } try { amount = Double.parseDouble(tf.getText()); } catch(Throwable ex) { throw new NumberFormatException("Insufficient funds", ex); } if(amount &lt; 0) { throw new NegativeAmountException("Negative amount not allowed for "); } else { return Double.parseDouble(tf.getText()); } } } </code></pre> <p>Account class code:</p> <pre><code>public class Account { private int id; private double balance; Account(int id, double balance) { this.id = id; this.balance = balance; } public int getId() { return id; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public void deposit(double amount) { balance += amount; } public void withdraw(double amount) { balance -= amount; } } </code></pre> <p>Exception code:</p> <pre><code>public class EmptyFieldException extends Exception { EmptyFieldException(String message) { super(message); } } </code></pre> <p>,</p> <pre><code>public class InsufficientFundsException extends Exception { InsufficientFundsException(String message) { super(message); } } </code></pre> <p>,</p> <pre><code>public class NegativeAmountException extends Exception { NegativeAmountException(String message) { super(message); } } </code></pre> <p>Can anyone help me? Where should I throw this exception? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload