0

I'm having some trouble getting my code to compile.

This is a method that uses the class Value to save text to a file

public void saveEventsToFile() throws Exception {
    String tmp = getEventsAsString();
    value = Value.makeString(tmp);
    Value.saveFile(value, "\\events" + "\\" + "YEAR" + "\\" + months[MONTH] + "\\" + DAY);
}

and this is part of a constructor of another object. I've got an actionlistener on a button (OK) and when that button is pressed, I want to call the saveEventsToFile method.

OK.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int h, m;
            h = (Integer) hourSpinner.getValue();
            m = (Integer) minuteSpinner.getValue();
            parentPanel.createNewEvent(parentPanel.selectedBox, parentWindow,       textPane.getText(), h, m);
            parentPanel.selectedBox.saveEventsToFile();
            dispose();
        }
    });

If I add throws Exception on actionPerformed my code does not compile, and without it I get "Unhandled exception" error on the arentPanel.selectedBox.saveEventsToFile(); line

How could I get this to compile? I've not had much experience with exceptions.

1
  • in eclipse mouse-over the error line and click "surround with try-catch" option Commented Mar 9, 2014 at 17:54

2 Answers 2

1

As donfuxx said:

OK.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int h, m;
            h = (Integer) hourSpinner.getValue();
            m = (Integer) minuteSpinner.getValue();
            parentPanel.createNewEvent(parentPanel.selectedBox, parentWindow,       textPane.getText(), h, m);
            try {
                parentPanel.selectedBox.saveEventsToFile();
            } catch (ExeptionThaIsThrownBySaveEventsMethod e) {
                // display error
            }
            dispose();
        }
    });
Sign up to request clarification or add additional context in comments.

3 Comments

Well, that did the trick. But one more question, when I don't specify a certain path the file is saved directly in the project folder. But when I specify a path within the project folder (e.g. "\\events" + "\\" + "YEAR" + "\\" + months[MONTH] + "\\" + DAY) I get a "file not found exception". How can I specify this path?
Please mark answer as correct and open a new question for this problem.
nvm I figured it out :)
0

First of all if you do not use any particular element of ActionEvent, move logic of the method in the body of class you have.

OK.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

                Integer h = (Integer) hourSpinner.getValue());
                Integer m = (Integer) minuteSpinner.getValue(); 

             saveEventsToFile(h,m)
              dispose();
        }
    });

private void saveEventsToFile(Integer hour, Integer minute) {

   if(hour == null || minute == null) {
     throw new IllegalArgumentException("The parameters must not be null.")
   }

   parentPanel.createNewEvent(parentPanel.selectedBox, parentWindow, textPane.getText(), h,m);

   try {
     parentPanel.selectedBox.saveEventsToFile();
   }catch(ExeptionThaIsThrownBySaveEventsMethod e) {
     throw new IllegalStateException(e);
   }
}

The things to remember here is that you should avoid to catch Exception as you may catch more than expected. This is important because after some exceptions application can proceed in other just need to be closed. In case the save cased an exception is might be possible that user can do something to retry the operation. In case the application must be close as can not longer operate you change use an unchecked (Runtime) Exception.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.