try/catch block not working in actionlistener

1.2k views Asked by At

I am working on 2 programs for school, one to take contact info and save it to a text file and the other is to read from the text file. All works perfectly but I have to add a try/catch block the program that takes input so that it would catch non-numeric entries in the age text field. I have been trying many different ways of doing this since yesterday and nothing has been working. Below is the code. If someone can point me in the right direction I would be very grateful because I feel like there is something fundamental I am not getting here. Thanks

  private class SaveData implements ActionListener{
    public void actionPerformed(ActionEvent e){

            String age1 = (String)ageField.getText();
            int age = Integer.parseInt(age1);

            try{
                int a = age;
            }
            catch(InputMismatchException e1){
                JOptionPane.showMessageDialog(null, "Please enter an     Integer");
            }

            String name = (String)nameField.getText();
            String email = (String)emailField.getText();
            String cell = (String)cellField.getText();  

            if(age>= 0 && age<=120){
                outputFile.println("Name: " + name);
                outputFile.println("Age: " + age);
                outputFile.println("Email: " + email);
                outputFile.println("Cell #: " +cell);
                outputFile.println("---------------------------");

                nameField.setText("");
                ageField.setText("");
                emailField.setText("");
                cellField.setText("");
            }
            else{
                JOptionPane.showMessageDialog(null, "You have entered an invalid age \n " +
                                        "Please enter an age between 0 and 120",
                                                "Age Entry Error",      JOptionPane.ERROR_MESSAGE);

                nameField.setText("");
                ageField.setText("");
                emailField.setText("");
                cellField.setText("");
            }//end else
        }//end actionperformed
    }//end actionlistener
2

There are 2 answers

1
codehitman On BEST ANSWER

So essentially I see three problems(including the ones that you aren't having problems with- yet):

  • The function parseInt throws an exception but you are not catching it because it is not in the try block.
  • You are catching the wrong exception so it will never be caught. You can read the Javadoc for that exception here.
  • The variable age is unreachable outside of the try/catch block due to scoping rules.

Here is how you should do it:

String age1 = (String)ageField.getText(); int age = -1;//always assign a variable a default value try{ age = Integer.parseInt(age1); } catch(NumberFormatException err){ JOptionPane.showMessageDialog(null, "Please enter a valid Integer!"); }

Final thought, if you are catching an exception, then you should display an error (which you are doing) and then return from that function. So essentially there should be a return in that catch block, as you don't want to continue to execute further code. Which will most likely fail because it expects a valid age value.

2
Sagar Koshti On

Simply write down int age = Integer.parseInt(age1); these lines inside try/catch block and catch for NumberFormatException Exception.