简体   繁体   中英

Catching exceptions with other classes

I am trying to get rid of this error in my AccountApplet class, it is

AccountApplet.java:109: error: unreported exception EmptyFieldException; must be caught or declared to be thrown
      getAmount(wttf);
           ^

here is a snippet from my AccountApplet class

  public void actionPerformed(ActionEvent e)
  {

    if (e.getSource() == dp)  //  Executes if deposit was clicked
    {
      //getAmount(dptf);
      status.setText("Deposit processed");

      refreshFields();

    }    

    if (e.getSource() == wt)  //  Executes if withdraw was clicked
    {
      getAmount(wttf);
      status.setText("Withdraw processed");

      refreshFields();
    }
  }  // End actionPerformed

  public void refreshFields()
  {
    NumberFormat fmt = NumberFormat.getCurrencyInstance();
    Account Account1 = new Account(1234, 1000.00);
    aitf.setText("" + Account1.getId());
    abtf.setText("" + fmt.format(Account1.getBalance()));
    // diplays accound id and balance in left text fields
    //should be called when the applet is first displayed and after each valid transaction
  }

  public double getAmount(JTextField tf) throws EmptyFieldException,
                                                NumberFormatException,
                                                NegativeAmountException
  {

    double withdraw;
    // try to parse 
    try {
        withdraw = Double.parseDouble(wttf.getText());
    } catch (Exception e) {
        // catch exception and do something about it  
        throw e;
    }
    // Next step


    return withdraw;
}  //  End       

and here is my account class

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;



public class Account extends Exception
{
  int id         = 1234;
  double balance = 1000.00;

  Account (int id, double balance)
  {
    id      = 1234;
    balance = 1000.00;
  }

  public int getId()
  {

    return id; 
  }

  public double getBalance()
  {
    return balance;   
  }

  public void setBalance(double balance) throws NegativeAmountException
  {

    // check for error and throw exception

  }

  public void deposit(double amount) throws NegativeAmountException
  {
    // check for error and throw exception
  }

  public void withdraw(double amount) throws NegativeAmountException,
                                             InsufficientFundsException
  {
    // check for error and throw exception
  }




}

You are calling getAmount(wttf); in actionPerformed method. This method throws 3 exceptions, so you have to declare them: public void actionPerformed(ActionEvent e) throws EmptyFieldException, NumberFormatException, NegativeAmountException . However, I am still not sure why class Account extends Exception...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM