通过抛出/捕获异常并尝试捕获块来严重搞砸我的代码

时间:2016-03-20 22:55:09

标签: java exception try-catch

好吧,我知道我抓住并抛出例外情况很糟糕,但我无法解决这些问题。我想我可以通过几种不同的方式来实现它们。任何人都可以指导我正确的方向(不寻找完整的答案,只是代码应该是什么样的)?它被卡在"抛出new();"线和捕获块。其他一切似乎都没有任何问题。它应该是对以前项目http://pastebin.com/sjR0BZBY的改进。你可以运行这个,如果你想看看程序应该做什么,如果有帮助的话。

public class Project6 extends JFrame
 {
 // Declaring variables
 private JLabel enterLabel, resultsLabel, errorLabel;
 private JTextField queryField, errorField;
 private JButton goB, clearB;
 private JTextArea textArea;
 private Container c;
 private JPanel panel;

 // main method
 public static void main(String[] args)
 {
   Project6 p6 = new Project6();
   p6.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   p6.setSize(new Dimension(455, 550));
   p6.setVisible(true);
 }

 // creating JFrame
 public Project6()
 {
   c = getContentPane();

   c.setLayout(new FlowLayout());

   setTitle("");
   c.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

   ButtonHandler handler = new ButtonHandler();

   enterLabel = new JLabel("Enter query here:");
   c.add(enterLabel); 

   queryField = new JTextField(35);
   c.add(queryField);

   goB = new JButton("GO!");
   c.add(goB);
   goB.addActionListener(handler);

   resultsLabel = new JLabel("Results:");
   c.add(resultsLabel);

   textArea = new JTextArea(20, 30);
   c.add(textArea);

   errorLabel = new JLabel("-------------Error messages:");
   c.add(errorLabel);

   errorField = new JTextField(35);
   c.add(errorField);

   clearB = new JButton("Clear");
   c.add(clearB);
   clearB.addActionListener(handler);
 }

 // ActionListener for buttons
 private class ButtonHandler implements ActionListener{  
  public void actionPerformed(ActionEvent e){

    // Go! button
    if (e.getSource() == goB){

         String str = queryField.getText();
         StringTokenizer tokens = new StringTokenizer(str, ", ");
         int numToks = tokens.countTokens();
         String[] tokenArray = new String[numToks];         
         int i = 0;

         // creating all tokenizing, validations, and the works. Try and bear    with it, it works but it's a rough trip
         while (tokens.hasMoreTokens()){

         // while loop to create tokens
         while (tokens.hasMoreTokens()){
         str = tokens.nextToken();         
         tokenArray[i] = str;         
         System.out.println(tokenArray[i]);
         i++;  }
         // start if verifications, everything else
         String query = tokenArray[3];
         String optok = tokenArray[4];
         String index = tokenArray[5];
         String qField = queryField.getText();
          try {
               if (qField.equals(""))
                  throw new MissingInputException();
               else {
               if (numToks != 6)
                  throw new IncorrectFormatException();
               else {               
               if (tokenArray[0].equalsIgnoreCase("Report"))
                {
                  if (tokenArray[1].equalsIgnoreCase("all"))
                   {
                     if (tokenArray[2].equals("where"))
                       {
                        if (tokenArray[3].equals("basePrice") || tokenArray[3].equals("quality") || tokenArray[3].equals("numInStock"))
                         {
                         if (tokenArray[4].equals("<") || tokenArray[4].equals(">") || tokenArray[4].equals(">=")||tokenArray[4].equals("<=")||tokenArray[4].equals("=="))
                          {
                           if (query.equals("basePrice"))
                             { BasePriceM(query, index, optok); }
                           else if (query.equals("quality"))
                            {  QualityM(query, index, optok); }
                           else if (query.equals("numInStock"))
                             { NumInStockM(query, index, optok); }
                           }
                          else
                          throw new InvalidOperatorException();
                         }
                        else
                         throw new InvalidLValueException();
                        }
                       else
                        throw new InvalidQualifierException();
                       }
                      else
                        throw new InvalidSelectorException();
                       }
                      else
                        throw new IllegalStartOfQueryException();
                       }
                      }


            } // end try
            catch(MissingInputException aa)
            {
               errorField.setText("Enter an expression");  
               queryField.setText("");
            }
            catch(IncorrectFormatException ab)
            {
               errorField.setText("Error, field too large or small");
               queryField.setText("");            
            }
            catch(IllegalStartOfQueryException ac)
            {
            errorField.setText("Error, report expected");  
            queryField.setText("");            
            }
            catch(InvalidSelectorException ad)
            {
               errorField.setText("Error, all expected");
               queryField.setText("");            
            }
            catch(InvalidQualifierException ae)
            {
                errorField.setText("Error, where expected");
                queryField.setText("");
            }
            catch(InvalidLValueException af)
            {
               errorField.setText("Error, basePrice, Quality, numInStock expected");
               queryField.setText("");           
            }
            catch(InvalidOperatorException ag)
            {
               errorField.setText("Error, < > >= <= == expected");
               queryField.setText("");            
            }
            catch(NumberFormatException af)
            {

            }                

        }// end of while loop
     } // end go button

      // clear all button
    if (e.getSource() == clearB)
      {
        queryField.setText("");
        textArea.setText("");
        errorField.setText("");   
      }
   }
  } // end buttons
  // methods for printing

 public void BasePriceM(String query, String index, String optok)
 {
 int pos = 0;
 int index1 = index.indexOf(".", pos);
 if (index1 > -1)
  {
   double b = Double.parseDouble(index);
     if (b > 0.05 && b < 5400.0)
       {printBasePrice(query, optok, b);}
      else{
        errorField.setText("Invalid query, must be between 0.05 and 5400.0");
        queryField.setText(""); }}
 }

 public void QualityM(String query, String index, String optok)
 {
 int pos2 = 0;
                      int index2 = index.indexOf(".", pos2);
                       if (index2 == -1)
                       {                         
                         int n = Integer.parseInt(index);
                         if (n > 0)
                           {                              
                             printBasePrice(query, optok, n);                              
                           }
                        else{
                        errorField.setText("Invalid query, must be greater than 0");
                        queryField.setText(""); }}
 }

 public void NumInStock(String query, String index, String optok)
 {
 int pos2 = 0;
                      int index2 = index.indexOf(".", pos2);
                       if (index2 == -1)
                       {                         
                         int n = Integer.parseInt(index);
                         if (n > 0)
                           {                              
                             printBasePrice(query, optok, n);                              
                           }
                        else{
                        errorField.setText("Invalid query, must be greater than 0");
                        queryField.setText("");} }
 }
 public void printQuality(String query, String optok, int p)
 {
   textArea.append("Search for " + query +" "+ optok +" "+ p + "\n");
 }
 public void printBasePrice(String query, String optok, double b)
 {
   textArea.append("Search for " + query +" "+ optok +" "+ b + "\n");
 }
 public void printNumInStock(String query, String optok, int n)
 {
   textArea.append("Search for " + query +" "+ optok +" "+ n + "\n");
 }
} // end program`enter code here`

0 个答案:

没有答案