2016-04-29 7 views
0

Ich versuche, die Befreiung von diesem Fehler in meiner AccountApplet Klasse zu erhalten, ist esFangen Ausnahmen mit anderen Klassen

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

hier ein Ausschnitt aus meiner AccountApplet Klasse ist

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  

und hier ist mein Kontoklasse

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 
    } 




} 
+0

https://docs.oracle.com/javase/tutorial/essential/exceptions/index.html Wie die Nachricht sagt: Die Ausnahme muss abgefangen werden oder deklariert, um geworfen zu werden. –

+0

hy ich verstehe nicht wirklich, was du meinst. Sie fangen den Fehler bereits ab: try { Zurückziehen = Double.parseDouble (wttf.getText()); } catch (Exception e) { // fangen Ausnahme und etwas dagegen tun werfen e; } aber du wirfst wieder. warum willst du es noch einmal werfen? – maxbit89

Antwort

0

Du getAmount(wttf); in actionPerformed Methode aufrufen. Diese Methode löst 3 Ausnahmen aus, daher müssen Sie sie deklarieren: public void actionPerformed(ActionEvent e) throws EmptyFieldException, NumberFormatException, NegativeAmountException. Ich bin mir jedoch immer noch nicht sicher, warum Klasse Konto erweitert Ausnahme ...

+0

wie deklariere ich sie? – HoodCoolege

+0

hinzufügen "wirft EmptyFieldException, NegativeAmountException" zu Methodensignatur .. Ich schrieb es an die Antwort – milan

+0

was meinst du es ändern? ändere es zu was? – HoodCoolege