设置例外

时间:2016-04-26 14:16:33

标签: java exception try-catch

我无法理解如何在作业中执行以下步骤,我是instructions

拥有Exception超类构造函数意味着什么?我会把它放在哪里?

这里只是EmptyFieldException类

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

public class AccountApplet extends JApplet implements ActionListener
{    
  //  For West
  public JLabel  ai       = new JLabel("Account ID ");
  public JTextField  aitf = new JTextField();
  public JLabel  ab       = new JLabel("Account Balance ");
  public JTextField  abtf = new JTextField();

  //  For East
  public JButton     dp   = new JButton ("Deposit");
  public JTextField  dptf = new JTextField();
  public JButton       wt = new JButton ("Withdraw");
  public JTextField  wttf = new JTextField();

  // For South
  public JLabel  status   = new JLabel("placeholder");  


  public void init()
  {
    this.setSize(400, 90);

    //----------------------
    //  Set up the Structure
    //----------------------

    Container      c = getContentPane();
    JPanel         b = new JPanel(new BorderLayout());
    JPanel      west = new JPanel(new GridLayout(2,2));
    JPanel      east = new JPanel(new BorderLayout());
    JPanel depo_with = new JPanel(new GridLayout(2,2));



    // Add BorderLayout to the container
    c.add(b);

    // Add everything to West
    b.add(west, BorderLayout.WEST);


    west.setBorder(new TitledBorder("Display Account Information"));
    west.add(ai);
    west.add(aitf);
    aitf.setEditable(false);
    west.add(ab);
    west.add(abtf);
    abtf.setEditable(false);

    // Add everything to EAST
    b.add(east, BorderLayout.EAST);

    east.setBorder(new TitledBorder("Deposit or Withdrawl Funds"));

    east.add(depo_with, BorderLayout.EAST);

    depo_with.add(dptf);
    depo_with.add(dp);
    depo_with.add(wttf);
    depo_with.add(wt);

    dp.addActionListener(this);
    wt.addActionListener(this);

    // Add everything to EAST
    b.add(status, BorderLayout.SOUTH);






  }  // End intit

  public void actionPerformed(ActionEvent e)
  {
    if (e.getSource() == dp)  //  Executes if deposit was clicked
    {
    }    

    if (e.getSource() == wt)  //  Executes if withdraw was clicked
    {
    }
  }  // End actionPerformed

  public void refreshFields()
  {
    // 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
  {
    return 5.0;
  }  //  End getAmount


} // End Class

这是我的应用程序,其中大部分工作已完成

FileLocator.toFileURL

1 个答案:

答案 0 :(得分:0)

使用super关键字调用超类的构造函数:

  

...将消息传递给异常超类构造函数。

public class MyException extends Exception { // Exception is the superclass

    public MyException() {
        super("My Message"); // call Exception's constructor
    }

}

在调用类构造函数时总是调用超类构造函数(可能Object除外)。

但是,如果超类构造函数没有参数,则不必显式调用super()

还应该注意,对超类构造函数的调用必须始终是构造函数中的第一个语句。