Exception handling 空白文本框的例外情况

Exception handling 空白文本框的例外情况,exception-handling,Exception Handling,我试图在弹出一条错误消息的地方做一个例外 错误:金额是必需的 当他们没有在方框中添加金额时。以下是我所拥有的: String output = "Gas Amount: $" + gasAmount + "\nCar Wash: " + price; try { throw new AmountException(); JOptionPane.showMessageDialog(null, output); } cat

我试图在弹出一条错误消息的地方做一个例外

错误:金额是必需的

当他们没有在方框中添加金额时。以下是我所拥有的:

String output = "Gas Amount: $" + gasAmount +
                      "\nCar Wash: " + price;
  try {
      throw new AmountException();
    JOptionPane.showMessageDialog(null, output);
  } 
  catch (AmountException ae) {
  JOptionPane.showMessageDialog(null, ae.toString(), "Error", JOptionPane.ERROR_MESSAGE);      
  }
      }
大概是这样的:

public class AmountException extends Throwable
{
  public AmountException()
  {
    super("Error: Amount is required!!");
  }
}
在代码中:

try
{
  if (gasAmount == null)
    throw new AmountException();
  JOptionPane.showMessageDialog(null, output);
}
catch (AmountException ae)
{
  JOptionPane.showMessageDialog(null, ae.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}

我确实将其添加到了我的代码中,但现在我的错误消息不会弹出。你确定你的“gasamunt”等于null吗?它是数字类的实例还是基本类型?也许它等于0。。。