954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Basic Exception Handling assistance

This is one of the two action listeners I have for two text fields. The exceptions to ensure the proper datatypes used is working. And the exception to make sure that int pos is within the array's range is working. The problem I am having is getting the out of bounds exception to work without calling either of the other two catches. If any more info is required don't hesitate to ask. TY

class ActListener1 implements ActionListener {

        public void actionPerformed( ActionEvent e) {
           int pos = 0;
            String strPos = input1.getText();
            String strValue = input2.getText();
           try{
             pos = Integer.parseInt( strPos );
          
            }catch(Exception ex1){
               String output1 = "Incorrect datatype entered for textfield 1\n" +
                            "Occured after pressing enter in the 1st textfield\n" +
                            "Enter appropriate datatype\n" + ex1.toString();
                    JOptionPane.showMessageDialog(null, output1);
            }
            try {
                    if (pos < MIN_INDEX || pos > MAX_INDEX) {
                        throw new Exception("Index out of bounds");
                    }
                } catch (Exception ob) {
                    String output2 = "Index is out of bounds\n" +
                            "Exception occured after pressing enter in the first textfield\n" +
                            "Re-enter a selection in the range of " + MIN_INDEX + " - " + MAX_INDEX;
                    ob.toString();
                    JOptionPane.showMessageDialog(null, output2);
                }
            try{

                double value = Double.parseDouble( strValue );
                numArray.insertNumberAtPos( value, pos );
            }catch(Exception ex2){
               String output2 = "Incorrect datatype entered for textfield 2\n" +
                            "Occured after pressing enter in the 1st textfield\n" +
                            "Enter appropriate datatype\n" + ex2.toString();
                    JOptionPane.showMessageDialog(null, output2);
            }
            // Display list and results
            TDisplay.setText(createListStr());
        }
    }
Afupi
Light Poster
40 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 
The problem I am having is getting the out of bounds exception to work without calling either of the other two catches.

I don't really get the above, but I have a few corrections to add.

First, don't use the toString method of the exception. Use the getMessage to print the error. String output2 = "Incorrect datatype entered for textfield 2\n" +
"Occured after pressing enter in the 1st textfield\n" +
"Enter appropriate datatype\n" + ex2.getMessage();

And secondly whenever you catch an exception, you print the error and then you continue with the execution of the program. If an exception occurred (user entered non numeric) you shouldn't continue with the rest of the program. So I would suggest to put a return statement at the end of each catch:

catch (Exception ob) {
                    String output2 = "Index is out of bounds\n" +
                            "Exception occured after pressing enter in the first textfield\n" +
                            "Re-enter a selection in the range of " + MIN_INDEX + " - " + MAX_INDEX;
                    ob.toString();
                    JOptionPane.showMessageDialog(null, output2);

<strong>return;</strong>
                }
javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

Thank you for the help javaAddict.

Afupi
Light Poster
40 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

Help please!!!
I am having this exception when I run my prog
[code]Exception in thread "main" java.lang.NullPointerException)[icode]
[code]at PatientReg.displaySQLErrors(PatientReg.java:423)[icode]
here are the codes for each line
[Code] private void displaySQLErrors(SQLException e) {
errorText.append("SQLException: " + e.getMessage() + "\n");
errorText.append("SQLState: " + e.getSQLState() + "\n");
errorText.append("VendorError: " + e.getErrorCode() + "\n");
}
[iCode]

at PatientReg.loadAccounts(PatientReg.java:68)
[Code] private void loadAccounts() {
Vector v = new Vector();
try {
rs = statement.executeQuery("SELECT * FROM PatientTable");

while (rs.next()) {
v.addElement(rs.getString("patienNo"));
}
} catch (SQLException e) {
displaySQLErrors(e);
}
PatientNumberList.setListData(v);
}[iCode]
at PatientReg.buildGUI(PatientReg.java:78)
[Code] loadAccounts();[iCode]
at PatientReg.main(PatientReg.java:443)
[Code] public static void main(String[] args) {
PatientReg patient = new PatientReg();
patient.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

patient.init();
patient.buildGUI();
}
}[iCode]
Please help me I going :icon_mad: with this project. If you need the whole code , I'll be very willing to post it.

JBeginer7891
Junior Poster in Training
59 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

Help please!!!
I am having this exception when I run my prog
[code]Exception in thread "main" java.lang.NullPointerException)[icode]
[code]at PatientReg.displaySQLErrors(PatientReg.java:423)[icode]
here are the codes for each line
[Code] private void displaySQLErrors(SQLException e) {
errorText.append("SQLException: " + e.getMessage() + "\n");
errorText.append("SQLState: " + e.getSQLState() + "\n");
errorText.append("VendorError: " + e.getErrorCode() + "\n");
}
[iCode]

at PatientReg.loadAccounts(PatientReg.java:68)
[Code] private void loadAccounts() {
Vector v = new Vector();
try {
rs = statement.executeQuery("SELECT * FROM PatientTable");

while (rs.next()) {
v.addElement(rs.getString("patienNo"));
}
} catch (SQLException e) {
displaySQLErrors(e);
}
PatientNumberList.setListData(v);
}[iCode]
at PatientReg.buildGUI(PatientReg.java:78)
[Code] loadAccounts();[iCode]
at PatientReg.main(PatientReg.java:443)
[Code] public static void main(String[] args) {
PatientReg patient = new PatientReg();
patient.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

patient.init();
patient.buildGUI();
}
}[iCode]
Please help me I going with this project. If you need the whole code , I'll be very willing to post it.

JBeginer7891
Junior Poster in Training
59 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

Start a new thread and use code tags.

When you start a code tag: [CODE], finish it with the same by adding the '/' character. You added this code tag:
[CODE]
and then you finish it like this: [/ICODE]

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You