Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3800428
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:53:27+00:00 2026-05-19T13:53:27+00:00

I am trying to call method that creates a file, however I am calling

  • 0

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 = " ";
        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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T13:53:28+00:00Added an answer on May 19, 2026 at 1:53 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working with ASP.net. I am trying to call a method that exists
I am trying to call a shell script that sets a bunch of environment
I'm trying to call a web service from Excel 2003 module. The way i've
When trying to call Close or Dispose on an SqlDataReader i get a timeout
I'm trying to call an Antlr task in my Ant build.xml as follows: <path
I'm trying to call a function after I load some XML into Actionscript, and
I am trying to call a webservice using ssl. How do i get the
I'm trying to call a php webservice using WCF. I googled some public php
I'm trying to call the SQL statement below but get the following error: System.Data.SqlClient.SqlException:
I'm trying to call the OpenThemeData (see msdn OpenThemeData ) function but I couldn't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.