Java 使用try/catch的正确方法

Java 使用try/catch的正确方法,java,try-catch,Java,Try Catch,我一直在努力让它工作一段时间,所以我正在寻求帮助。在我下面的代码中,我的异常不是打印自定义错误,而是崩溃,我不知道为什么。任何帮助都将不胜感激 try { { JButton btnStart = new JButton("Start"); btnStart.addActionListener(new ActionListener() { public void actionPerformed(Act

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

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();
    }
取消平铺的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)
添加行

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");
                }

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

错误信息是什么?请提供堆栈跟踪。StackTrace请…………为什么要将运行时异常捕获为最外部的捕获语句?堆栈跟踪在上面。我不知道为什么我会在那里看到它,我对这个有点陌生。非常感谢。我现在可以睡觉了!:)但是Integer.parseInt语句仍然嵌入在外部try块中。他只需要在最外层的catch块中添加S.O.P。这与打印堆栈跟踪有什么不同。无论如何,捕获NumberFormatException只是一个特例。除非您打算对运行时异常进行处理,否则不能捕获运行时异常。