ERROR: unreported exception java.io.IOException; must be caught or declared to be thrown

ERROR: unreported exception java.io.IOException; must be caught or declared to be thrown

我几乎完成了这项作业,但我一直收到错误 “unreported exception java.io.IOException; must be caught or declared to be thrown”我是在乱写代码还是遗漏了一大块代码?感谢您的宝贵时间!

import java.io.*;
import java.util.*;
import java.text.*;

public class database1 {

    static Scanner console = new Scanner(System.in);

    public static void main(String[] args) throws FileNotFoundException {
        String empnumber;
        String firstname;
        String lastname;
        String city;
        String state;
        String zipcode;
        String jobtitle;
        int salary;
        int max = 200000;
        int counter = 0;

        FileWriter ryyt = new FileWriter("c:\EmployeeData.txt");

        BufferedWriter out = new BufferedWriter(ryyt);

        while (counter < 9) {
            System.out.print("Enter the employee number ...... ");
            empnumber = console.next();

            System.out.print("Enter employee's first name .... ");
            firstname = console.next();

            //firstname = Character.toUpperCase(firstname.charAt(0)) + 
            firstname.substring(1);

            System.out.print("Enter employee's last name ..... ");
            lastname = console.next();
            lastname = Character.toUpperCase(lastname.charAt(0))
                    + lastname.substring(1);

            System.out.print("Enter employee's city .......... ");
            city = console.next();
            city = Character.toUpperCase(city.charAt(0)) + city.substring(1);

            System.out.print("Enter employee's state ......... ");
            state = console.next();
            String upperstate = state.toUpperCase();

            System.out.print("Enter employee's zip code ...... ");
            zipcode = console.next();

            System.out.print("Enter employee's job title ..... ");
            jobtitle = console.next();
            jobtitle = Character.toUpperCase(jobtitle.charAt(0))
                    + jobtitle.substring(1);

            System.out.print("Enter employee's salary ........ ");
            salary = console.nextInt();

            while (salary > max) {
                if (salary > max) {
                    System.out.println(
                            "Salary is over the maxium allowed, re-enter please ...");
                    System.out.print("Enter employee's salary ........ ");
                    salary = console.nextInt();
                } else {
                    System.out.println("Thank you please enter the next employee!");
                }
            }

            System.out.println();

            out.write(empnumber + ",");
            out.write(firstname + ",");
            out.write(lastname + ",");
            out.write(city + ",");
            out.write(upperstate + ",");
            out.write(zipcode + ",");
            out.write(jobtitle + ",");
            out.write(salary + ",");
            out.newLine();

            counter = counter + 1;
        }

        out.close();
    }
}

你必须像这样使用 throws IOException :

public static void main(String[] args) throws FileNotFoundException, IOException 

或者你可以用 try{}catch(...){} 包围你的语句:

try {
    //Your code ...
} catch (IOException ex) {
    //exception 
}