java - 抛出你自己的异常

标签 java

我是 Java 新手,一直在开发一个转换器,该转换器将采用十进制数并将其转换为二进制,反之亦然。我想实现异常处理,但很难完全理解这个概念。我希望程序捕获 NumberFormatException 并抛出我在单独的类中创建的 NotInBinaryException。我能够抛出新的异常,但不确定如何成功捕获异常以显示 JOptionPane ,该异常将显示在程序上,提示用户输入二进制格式的数字,同时清除错误并将焦点设置在存在错误的文本字段上。到目前为止,这是我创建的代码。如果有任何帮助让我重回正轨,我将不胜感激。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class ImprovedBaseGui extends JFrame implements ActionListener
{
 private JTextField txtBaseTen;
 private JTextField txtBaseTwo;
 private JButton btnBaseTen;
 private JButton btnBaseTwo;
 private JButton btnClear;
 public ImprovedBaseGui()
{
    this.setTitle("Base 10/2 Converter");    
    Container canvas = this.getContentPane();

    canvas.add(createCenterPanel(), BorderLayout.CENTER);
    canvas.add(createSouthPanel(), BorderLayout.SOUTH);


    this.setResizable(false);
    this.setSize(475, 150);
    this.setLocation(800, 500);
    this.setVisible(true);
    this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
}

private JPanel createSouthPanel()
    {
        JPanel pnlSouth = new JPanel();

        btnBaseTen = new JButton("Base 10");
        btnBaseTen.addActionListener(this);
        btnBaseTen.setToolTipText( "Use to convert Base 2 to Base 10" );
        btnBaseTen.setBackground(Color.CYAN);
        pnlSouth.add(btnBaseTen);

        btnBaseTwo = new JButton("Base 2");
        btnBaseTwo.addActionListener(this);
        btnBaseTwo.setToolTipText( "Use to convert Base 10 to Base 2" );
        btnBaseTwo.setBackground(Color.YELLOW);
        pnlSouth.add(btnBaseTwo);

        btnClear = new JButton("Clear");
        btnClear.addActionListener(this);
        btnClear.setBackground(Color.RED);
        pnlSouth.add(btnClear);

        return pnlSouth;
    }

private JPanel createCenterPanel()
    {
        JPanel pnlCenter = new JPanel();
        pnlCenter.setLayout(new GridLayout(2,2));

        pnlCenter.add(wrapMeInAPanel(new JLabel ("Base 10")));
        txtBaseTen = new JTextField(16);
        txtBaseTen.setBackground(Color.YELLOW);
        pnlCenter.add(wrapMeInAPanel(txtBaseTen));

        pnlCenter.add(wrapMeInAPanel(new JLabel("Base 2")));
        txtBaseTwo = new JTextField(16);
        txtBaseTwo.setBackground(Color.CYAN);
        pnlCenter.add(wrapMeInAPanel(txtBaseTwo));

        return pnlCenter;
    }

    public static void main(String[] args)
    {
      new ImprovedBaseGui();
    }

    @Override
    public void actionPerformed(ActionEvent e)
        {

            if(e.getSource() == btnClear)
            {
                txtBaseTen.setText("");
                txtBaseTwo.setText("");
            }
            if(e.getSource() == btnBaseTwo)
            {
                try
                    {
txtBaseTwo.setText(Integer.toBinaryString(Integer.parseInt(txtBaseTen.getText())));
                    }
                catch(NumberFormatException err)
                    {
JOptionPane.showMessageDialog(this, txtBaseTen.getText()+"");
                    txtBaseTen.setText("");
                    txtBaseTen.grabFocus();
                    }

            }
            if(e.getSource() == btnBaseTen)
            {
                try
                    {
txtBaseTen.setText(Integer.toString(Integer.parseInt(txtBaseTwo.getText(), 2)));
            }
            catch(NumberFormatException err)
            {
            throw new NotInBinaryException();
                }

            }

        }

     private JPanel wrapMeInAPanel(Component c)
            {
                JPanel panel = new JPanel();
                panel.add(c);
                return panel;
            }
}

最佳答案

如果您的方法无法处理二进制解析中的 NumberFormatException,则仅应抛出 NotInBinaryException。但它可以处理它,你也应该处理它。您在这里不需要 NotInBinaryException

只需按照与 txtBaseTen 案例中的处理方式类似的方式处理 NumberFormatException,尽管您可能希望为两者选择更用户友好的错误消息。

try
{
    txtBaseTen.setText(Integer.toString(Integer.parseInt(txtBaseTwo.getText(), 2)));
}
catch(NumberFormatException err)
{
    JOptionPane.showMessageDialog(this, txtBaseTwo.getText()+"");
    txtBaseTwo.setText("");
    txtBaseTwo.grabFocus();
}

关于java - 抛出你自己的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15279478/

相关文章:

java - JRE 在不应出现的情况下给出 NullPointerException

java - 如何从 ZIP 存档中读取文件

java - 小程序不显示,但也没有错误

java - 通过定时器设置 JDialog 不透明度

java - 如何在java中重新采样图像并保持其物理尺寸?

java - 如何在 android studio 中找到数组中项目的编号?

java - 工资柜台最终工资不增加?

java - 使用 XPATH 在 Java 中解析 XML 节点

java - 我使用 Java HashMap 越多,性能下降得越多 - 即使大小稳定

java - Json 文件到 proto