gpt4 book ai didi

java - 我收到 "unreported exception ioexception; must be kept or declared to be thrown"

转载 作者:行者123 更新时间:2023-11-30 06:24:39 26 4
gpt4 key购买 nike

我收到错误“未报告的异常 ioException;必须保留或声明为抛出”。这是提供的路径或 try - catch block 中的错误。

import java.io.*;
import java.util.regex.*;

class RegexMobileExtractor {

public static void main(String[] args) {
try {
Pattern p = Pattern.compile("(0|9)?[7-9][0-9]{9}");
PrintWriter pw = new PrintWriter("C:\\Users\\HP\\Desktop\\CODE\\JAVA_EX\\copy\\output.txt");
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\HP\\Desktop\\CODE\\JAVA_EX\\copy\\input.txt"));

//PrintWriter pw = new PrintWriter("output.txt");
//BufferedReader br = new BufferedReader(new FileReader("input.txt"));

String line = br.readLine();
while( line!= null) {
Matcher m = p.matcher(line);
while(m.find()) {
pw.println(m.group());
}

line = br.readLine();
}

pw.flush();
pw.close();
//br.close();
} catch (FileNotFoundException obj) {
System.out.println("errr occured");
}
}
}

最佳答案

这行代码:br.readLine();可能会抛出IOException。这是被破解的异常,编译器强制您处理它,这就是为什么您必须添加额外的catch block :

catch (IOException e) {
e.printStackTrace();
}

关于java - 我收到 "unreported exception ioexception; must be kept or declared to be thrown",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47457680/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com