0

I am relatively new to exception handling and this is my problem: I am writing an application in Java where users have to enter data in various fields on various panels, all on the same frame. Each panel has a save button. After they press "Save", objects are created with the data as attributes. Each setter method checks the data (strings have to be less than a certain length, integers have to fall in certain intervals etc) and if it's not OK then exceptions are thrown (I have created my own BadDataException type). These exceptions are thrown all the way back to the frame, which is supposed to handle(display) them.

So here is the question: if a user enters bad data in more than one field, how can I show him all the fields where he's making mistakes? Right now all I get is the message for the first thrown exception (I know it's supposed to behave like that). Do I have to add a some checks in the panels themselves (I don't think it's quite right to do that) or is there any way I can "log" all the exceptions after he presses "Save"? If I can do that then I will be able to display them all in a label on the main frame.

I guess the problem is clear enough, but I have written some simple code to illustrate.

public class MyFrame extends JFrame { private JTextField stringFieldOne; private JTextField stringFieldTwo; public MyFrame() { stringFieldOne = new JTextField(); this.add(stringFieldOne); stringFieldTwo = new JTextField(); this.add(stringFieldTwo); JButton saveButton = new JButton(); saveButton.addActionListener(new SaveButtonListener()); this.add(saveButton); }//constructor private void save() { SaveObject obj = new SaveObject(); try { obj.setStringOne(stringFieldOne.getText()); obj.setStringTwo(stringFieldTwo.getText()); } catch (BadDataException bde) { //what goes here??? } }//method class SaveButtonListener implements ActionListener { public void actionPerformed(ActionEvent evt) { save(); }//method }//class }//class public class SaveObject { private String theStringOne; private String theStringTwo; public void setStringOne(String someString) throws BadDataException { if (someString.length() <= 20) { theStringOne = someString; } else { throw new BadDataException("The string you introduced has more than 20 characters"); } }//method public void setStringTwo(String someString) throws BadDataException { if (someString.length() <= 5) { theStringTwo = someString; } else { throw new BadDataException("The string you introduced has more than 5 characters"); } }//method }//class 
1
  • @chiccodoro: Ok, I see what you mean. I'll be able to start the exception handling for my app now. Thanks! Commented Apr 28, 2014 at 10:31

1 Answer 1

2

Throwing an exception in the set methods might not be a good fit for your requirement. An exception breaks the normal control flow and enters the exception handling control flow. That is, all your other data checks will never be executed.

Normally for things like that you would validate all data and collect the validation results in a list or so. Only after all the validation is done, you throw an exception in the case that the list is not empty.

Example:

private void save() { SaveObject obj = new SaveObject(); obj.setStringOne(stringFieldOne.getText()); obj.setStringTwo(stringFieldTwo.getText()); List<string> validationErrors = obj.validate(); if (validationErrors.length > 0) { // do whatever you need to do, e.g. throw an exception, or render the // messages. } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.