2013-11-22 15 views
0

我一直在努力讓它工作一段時間,所以我在尋求幫助。在我的下面的代碼中,我的例外是不打印我的自定義錯誤,而只是崩潰,我不知道爲什麼。任何幫助將不勝感激。使用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堆棧跟蹤如下:

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) 
+4

什麼是錯誤信息?請提供堆棧跟蹤。 – Vidya

+0

StackTrace Please ............. –

+0

爲什麼你捕捉運行時異常作爲最外層的catch語句? –

回答

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

重複上面的代碼中的所有這些事件。

+0

謝謝sooooooo多。我現在可以去睡覺了! :) –

+0

但是仍然將Integer.parseInt語句嵌入到外部try塊中。他只需要在最外面的catch塊中添加S.O.P。 –

+0

與打印堆棧跟蹤有什麼不同?無論如何,捕獲NumberFormatException只是一個特例。除非你有意使用它,否則一定不要捕捉運行時異常。 –