Where do I go after an exception in a GUI?

3198 views java
4

Total beginner question, but I'm having a little conceptual difficulty seeing how an exception handler can be used within a Java GUI.

I have the following code on an event listener, waiting for a button to be pressed. Basically, if they enter something that is not an integer then it can't be a Customer ID and so I have an exception handler checking if it can be parsed as an integer. If it can't, then it puts an error message in to a label. However, what do I do from there? At the moment, it obviously continues to the finally block and enters the CustomerID as 0 as that's what it was initialised as.

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    int custID = 0;
    String custName = "";
    String custAddress = "";
    String custEmail = "";
    String custPhone = "";
    try{
    custID = Integer.parseInt(this.custIDField.getText());
    }
    catch (NumberFormatException e){
        errorMess.setText("Not a valid customer ID");
    }
    finally{
        custName = this.nameField.getText();
        custAddress = this.addressField.getText();
        custEmail = this.emailField.getText();
        custPhone = this.phoneField.getText();
        comp.addCust(custID, custName, custAddress, 
            custEmail, custPhone);
        custListArea.append(custID + ": " +custName);
        custListArea.append("\n");
        addressField.setText(null);
        emailField.setText(null);
        phoneField.setText(null);
        nameField.setText(null);
        custIDField.setText(null);
    }
} 

What's the proper way of dealing with this and getting the button event handler to go back to waiting for the correct input?

answered question

2 Answers

6

Get rid of the finally block. What you should do is to notify the user of erroneous input, and then not allow the input to be accepted until what the user enters is valid.

posted this
7

You can simply do:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    int custID = 0;
    String custName = "";
    String custAddress = "";
    String custEmail = "";
    String custPhone = "";
    try {
        custID = Integer.parseInt(this.custIDField.getText());

        custName = this.nameField.getText();
        custAddress = this.addressField.getText();
        custEmail = this.emailField.getText();
        custPhone = this.phoneField.getText();
        comp.addCust(custID, custName, custAddress, custEmail, custPhone);
        custListArea.append(custID + ": " + custName);
        custListArea.append("\n");
        addressField.setText(null);
        emailField.setText(null);
        phoneField.setText(null);
        nameField.setText(null);
        custIDField.setText(null);
    } catch (NumberFormatException e) {
        errorMess.setText("Not a valid customer ID");
    }
}

If there's a NumberFormatException when calling parseInt it will throw the exception, and it won't execute your code where it sets the text to the fields. If there's no exception it will update the text in the fields.

The finally block will always execute and therefore will also execute when the exception is thrown.

posted this

Have an answer?

JD

Please login first before posting an answer.