开发者

call method that creates a file from the Action Performed

开发者 https://www.devze.com 2023-02-06 13:06 出处:网络
I am trying to call method that creates a file, however I am calling that method from the Action Performed which simply can not have throws IOException...

I am trying to call method that creates a file, however I am calling that method from the Action Performed which simply can not have throws IOException...

Here is the code:

/* ACTION PERFORMED**/
public void actionPerformed(ActionEvent evt){
    Object source = evt.getSource();
    if (source == add)
    {
        String mothername = "开发者_Python百科 ";
        String fathername = " ";
        String motherphone = " ";
        String fatherphone = " ";

        Patient patient = new Patient(...));

        printPatients(patient);

        System.out.println("past printing patient");

        writetoFile(patient); //giving an error
    }

    if (source == uadd)
    {
        Patient patient = new Patient(...));

        printPatients(patient);

        writetoFile(patient); //giving an error
    }
}

//This is the method I am trying to call

public static void writetoFile(Patient p) throws IOException
{
    RandomAccessFile inout = new RandomAccessFile("PatientsInfo.dat", "rw");

    inout.seek(inout.length());
    inout.writeUTF(p.getName());
    inout.writeUTF(p.getAge());
    inout.writeUTF(p.getGender());
    inout.writeUTF(p.getSiblings());
    inout.writeUTF(p.getID());
    inout.writeUTF(p.getNationality());
    inout.writeUTF(p.getCivilStatus());
    inout.writeUTF(p.getProfession());
    inout.writeUTF(p.getPhone1());
    inout.writeUTF(p.getPhone2());
    inout.writeUTF(p.getEmail());
    inout.writeUTF(p.getMotherName());
    inout.writeUTF(p.getFatherName());
    inout.writeUTF(p.getMotherPhone());
    inout.writeUTF(p.getFatherPhone());
    inout.writeUTF(p.getMedication());
    inout.writeUTF(p.getDoctorsName());
    inout.writeUTF(p.getFrequency());
    inout.writeUTF(p.getPrice());
    System.out.println("names and sentinel value sent to file Countries.dat");

    inout.close();
}

//The error is in the two blue lines, and the error it shows is:

Error: C:\Users\Pedro Quintas\Documents\Documents and Work
\Escola\Computer Science\Programs\Dossier\AddPatient.java:362:
unreported exception java.io.IOException; must be caught or
declared to be thrown

Please tell me what to change


The answer is in the error message :) You have to handle your exceptions. They aren't there just to blow things up when things go slightly askew -- they are there for you to figure out how you want to handle your errors when they happen. This means that you have to think about which portions of your program are going to handle error conditions, and which portions of your program are going to assume that errors don't happen.

You might want your actionPerformed() method to place an error dialog box on screen to alert the user that the 'save' button in fact threw away all their work. In that case, wrap all those calls to writeToFile() in try/catch blocks and handle appropriately.

You might want your writeToFile() to log a message to the log4j instance logging your application, or simply spit something to standard error or standard out when writing fails. In that case, undelcare throws IOException from your writeToFile(), wrap the contents of the method in a try/catch block and handle appropriately.

Handling errors is, at least in my experience, most of the code of most applications. It's a pity schools don't teach it better, but here's your opportunity to learn what design tradeoffs you have by trying both of my suggestions here and noticing the effects elsewhere in the program.

0

精彩评论

暂无评论...
验证码 换一张
取 消