使用try / catch的正确方法

时间:2013-11-22 06:29:24

标签: java try-catch

我一直在努力让这个工作一段时间,所以我正在寻求帮助。在我的下面的代码中,我的异常不是打印我的自定义错误,而是它只是崩溃,我不知道为什么。任何帮助将不胜感激。

try {
        {
            JButton btnStart = new JButton("Start");
            btnStart.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    name = JOptionPane.showInputDialog("Enter     your name");

                    answer = JOptionPane.showInputDialog("What is 2 + 3?");
                    userAnswer = Integer.parseInt(answer);
                    try {
                        if (userAnswer == 5) {
                            JOptionPane.showMessageDialog(null, "Good!!");
                            score = score + awarded;
                        }

                        else {
                            JOptionPane.showMessageDialog(null,
                                    "Not Even Close!");
                        }
                    } catch (NumberFormatException e) {
                        System.out.println("enter a number");
                    }
                    answer = JOptionPane.showInputDialog("What is 2 x 8?");
                    userAnswer = Integer.parseInt(answer);
                    try {
                        if (userAnswer == 16) {
                            score = score + awarded;
                            JOptionPane.showMessageDialog(null, "Super!!");

                        }

                        else {
                            JOptionPane.showMessageDialog(null,
                                    "Not Even Close!");
                        }
                    } catch (NumberFormatException e) {
                        System.out.println("enter a number");
                    }

                    answer = JOptionPane
                            .showInputDialog("What is 144 / 12?");
                    userAnswer = Integer.parseInt(answer);
                    try {
                        if (userAnswer == 12) {
                            JOptionPane.showMessageDialog(null,
                                    "Excellent!!");
                            score = score + awarded;
                        }

                        else {
                            JOptionPane.showMessageDialog(null,
                                    "Not Even Close!");
                        }
                    } catch (NumberFormatException e) {
                        System.out.println("enter a number");
                    }

                    JOptionPane.showMessageDialog(null,
                            "Thanks for playing " + name
                            + " Your score is " + score);

                }
            });
            btnStart.setIcon(new ImageIcon(
                    MathJFrame.class
                    .getResource("/com/sun/java/swing/plaf/windows/icons/Computer.gif")));
            btnStart.setBounds(241, 179, 141, 65);
            contentPane.add(btnStart);
        }
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

Deatiled StackTrace如下:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at MathJFrame$4.actionPerformed(MathJFrame.java:105)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

1 个答案:

答案 0 :(得分:1)

添加行

userAnswer = Integer.parseInt(answer);
每次进行输入时,在你的try-catch语句中

                try {
                    userAnswer = Integer.parseInt(answer);//Add this line here 
                    if (userAnswer == 5) {
                        JOptionPane.showMessageDialog(null, "Good!!");
                        score = score + awarded;
                    }

                    else {
                        JOptionPane.showMessageDialog(null,
                                "Not Even Close!");
                    }
                } catch (NumberFormatException e) {
                    System.out.println("enter a number");
                }

在代码中对所有此类事件重复上述步骤。