![](/img/trans.png)
[英]Error “unreported exception java.io.ioexception must be caught or declared to be thrown” in Java class
[英]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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.