Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nested try-catch-finally block? 3

Status
Not open for further replies.

chunkII123

IS-IT--Management
Mar 18, 2009
78
US
Alright, I have a action performed on okButton click, essentially I need it to be possibly a nested try-catch block. With that said, heres whats going on - Right now it looks like this

Code:
 try {
            if (memNumField != null && matchesMemNumField != null) {
                if (dateScheduledField != null) {
                    int memberNumber = Integer.valueOf(memNumField.getText());
                    int matchesMemberNumber = Integer.valueOf(matchesMemNumField.getText());
                    String date = dateScheduledField.toMySQLFormat();
                    scheduledMatches.updateMatches(memberNumber, matchesMemberNumber, date);
                }
            }
        } catch (Exception exc) {
            exc.printStackTrace();
            String warningMessage = "The member number, the matches member number, or the date scheduled was left blank, please try again.";
            WarningMessage warning = new WarningMessage(true, warningMessage, WarningMessage.NON_CRITICAL_ERROR);
            warning.setVisible(false);
            JOptionPane.showMessageDialog(this, warningMessage, "Error", JOptionPane.INFORMATION_MESSAGE);
        }
        if (parent instanceof MatchedMembersForm) {
            MatchedMembersForm m = (MatchedMembersForm) parent;
            m.loadTables();
        }

        doClose(RET_OK);

    }

This makes it functional, but not exactly how I need it or want it; I have been having one heck of a time for the past three days trying to get it to work, but it's not quite working - I need it to throw an exception if the memNumField is blank, the matchesMemNumField, or the dateSchedule field == null. Or any combination of them, like, the dates missing and the other two are filled, etc... or whatever.

Any help is much appreciated, and thank you for reading.

Beware of hackers bearing executables. Happy Hunting. 'irc.2600.net'
 
With the NOT NULL checks that you have in place no exception will be ever thrown!

You should have something like this:

Code:
try {
  if (memNumField == null || matchesMemNumField == null || dateScheduledField == null) {
    throw new Exception("Fields null");
}
                else {
                    int memberNumber = Integer.valueOf(memNumField.getText());
                    int matchesMemberNumber = Integer.valueOf(matchesMemNumField.getText());
                    String date = dateScheduledField.toMySQLFormat();
                    scheduledMatches.updateMatches(memberNumber, matchesMemberNumber, date);
                }
            }
}
catch(Exception e) {
  exc.printStackTrace();
            String warningMessage = "The member number, the matches member number, or the date scheduled was left blank, please try again.";
            WarningMessage warning = new WarningMessage(true, warningMessage, WarningMessage.NON_CRITICAL_ERROR);
            warning.setVisible(false);
            JOptionPane.showMessageDialog(this, warningMessage, "Error", JOptionPane.INFORMATION_MESSAGE);
        }
        if (parent instanceof MatchedMembersForm) {
            MatchedMembersForm m = (MatchedMembersForm) parent;
            m.loadTables();
        }

        doClose(RET_OK);
}

Hope this is what you are looking for!
 
Thant was quick, and i'll check right now, keep you updated. Thanks again.

Beware of hackers bearing executables. Happy Hunting. 'irc.2600.net'
 
snit --

works wonderfully, is there a way to get it to throw a different message for each null value? For instance I left the memNumField blank on accident, but matchesMemNumField and dateScheduled != null, it sends the user an error stating what/which field(s) were blank?

thanks again.

Beware of hackers bearing executables. Happy Hunting. 'irc.2600.net'
 
Sure! Just change the try block to send the appropriate message along with the exception.

Code:
try {
if(memNumField == null || memNumField.equals(""))
   throw new Exception("The member number was left blank, please try again."); 

if(matchesMemNumField == null )
   throw new Exception("The matches member number was left blank, please try again.");

if(dateScheduledField == null)
  throw new Exception("The date scheduled was left blank, please try again.");
 //rest of the code here
} catch(Exception e) {
    exc.printStackTrace();
            String warningMessage = [COLOR=#ff0000]e.getMessage();[/color]
            WarningMessage warning = new WarningMessage(true, warningMessage, WarningMessage.NON_CRITICAL_ERROR);
            warning.setVisible(false);
            JOptionPane.showMessageDialog(this, warningMessage, "Error", JOptionPane.INFORMATION_MESSAGE);
        }
        if (parent instanceof MatchedMembersForm) {
            MatchedMembersForm m = (MatchedMembersForm) parent;
            m.loadTables();
        }

        doClose(RET_OK);
}

Although this would work, as a good design practice I would suggest you to write a field empty/blank validator class and have it throw a custom exception like FieldBlankException rather than using the java.lang.Exception class
 
Why throw an exception if you're planning to catch it inmediatly? Usually exceptions are used when they're caught anywhere else.

Code:
String errorMessage = null;
if(memNumField == null || memNumField.equals(""))
   errorMessage = "The member number was left blank, please try again."; 
else if(matchesMemNumField == null )
   errorMessage = "The matches member number was left blank, please try again.";
else if(dateScheduledField == null)
  errorMessage = "The date scheduled was left blank, please try again.";

If (errorMessage != null) {
      WarningMessage warning = new WarningMessage(true, errorMessage, WarningMessage.NON_CRITICAL_ERROR);
            warning.setVisible(false);
            JOptionPane.showMessageDialog(this, warningMessage, "Error", JOptionPane.INFORMATION_MESSAGE);
        }
        if (parent instanceof MatchedMembersForm) {
            MatchedMembersForm m = (MatchedMembersForm) parent;
            m.loadTables();
        }

        doClose(RET_OK);
} else 
{ normal code }



Cheers,
Dian
 
Since you are developing a Swing application, why not add a DocumentListener on each field that should not be empty. If the field is empty then the DocumentListener could disable the OK button. In that case the user can only click the OK button when all conditions are met; your program doesn't have to do all these null checks nor throw error messages around.
 
Thanks everyone, you got it working for me.

-Josh

Beware of hackers bearing executables. Happy Hunting. 'irc.2600.net'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top