java:unreported exception java.io.FileNotFoundException;必须被抓或宣布被抛出[重复]

时间:2022-11-29 20:18:56

This question already has an answer here:

这个问题在这里已有答案:

Why am I getting a "must be caught or declared to be thrown" error with this code ? All I want is to test a bunch of code by pasting it into a new java program what is the easiest way to bunch of code ?

为什么我在这段代码中遇到“必须被捕或被声称被抛出”的错误?我想要的是通过将它粘贴到一个新的java程序来测试一堆代码,这是一堆最简单的代码方法吗?

import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(new File("C:\\Users\\User\\Selenium\\scrapjv\\interface\\NASDAQlist.txt"));
        List<String> symbolList = new ArrayList<String>();
        while (sc.hasNextLine()) {
            symbolList.add(sc.nextLine());
        }

        PrintWriter logput = new PrintWriter("C:\\Users\\User\\Selenium\\scrapjv\\interface\\log.txt", "UTF-8");
        for (String symb : symbolList) {
            System.out.println(symb);
        }
        logput.close();
    }
}

2 个解决方案

#1


2  

Some of the methods you're calling can throw FileNotFoundException if the file isn't found:

如果找不到该文件,您正在调用的某些方法可能会抛出FileNotFoundException:

 public Scanner(File source) throws FileNotFoundException
 public PrintWriter(String fileName) throws FileNotFoundException

Java's compiler checks that some thrown exceptions -- those other than RuntimeException and its subclasses -- are either caught or declared thrown. Compilation will fail otherwise. This helps find some errors at compile-time, before the program is ever run.

Java的编译器会检查一些抛出的异常 - 除RuntimeException及其子类之外的异常 - 被捕获或声明抛出。否则编译将失败。这有助于在程序运行之前在编译时发现一些错误。

One option is to declare your calling function to throw the exception or a superclass:

一种选择是声明你的调用函数抛出异常或超类:

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

A better option in this case is to catch the exception and do something with it. For example, here's how you can do that for the Scanner() exception:

在这种情况下,更好的选择是捕获异常并对其执行某些操作。例如,以下是如何为Scanner()异常执行此操作:

    File inFile = new File("C:\\Users\\User\\Selenium\\scrapjv\\interface\\NASDAQlist.txt");
    try {
        Scanner sc = new Scanner( inFile );
        List<String> symbolList = new ArrayList<String>();
        while (sc.hasNextLine()) {
            symbolList.add(sc.nextLine());
        }
    }
    catch ( FileNotFoundException e ) {
        System.out.println("Could not find file: " + inFile.getAbsolutePath());
    }

#2


1  

Your two Scanner declaration lines have a chance to throw an exception, which are basically errors that happen after the code is executed (because of this they are sometimes called runtime errors). Because the compiler knows that your code might make a FileNotFoundException happen, it requires you to catch the exception.

您的两个Scanner声明行有机会抛出异常,这些异常基本上是在执行代码后发生的错误(因此有时称为运行时错误)。因为编译器知道您的代码可能会发生FileNotFoundException,所以它需要您捕获异常。

This is done by enclosing the code in a try-catch block.

这是通过将代码包含在try-catch块中来完成的。

try {

    Scanner sc = new Scanner(new File("C:\\Users\\User\\Selenium\\scrapjv\\interface\\NASDAQlist.txt"));
    List<String> symbolList = new ArrayList<String>();
    while (sc.hasNextLine()) {
        symbolList.add(sc.nextLine());
    }


    PrintWriter logput = new PrintWriter("C:\\Users\\User\\Selenium\\scrapjv\\interface\\log.txt", "UTF-8");
    for (String symb : symbolList) {
        System.out.println(symb);
    }
    logput.close();


} catch (java.io.FileNotFoundException ex)
{
    ex.printStackTrace();
}

#1


2  

Some of the methods you're calling can throw FileNotFoundException if the file isn't found:

如果找不到该文件,您正在调用的某些方法可能会抛出FileNotFoundException:

 public Scanner(File source) throws FileNotFoundException
 public PrintWriter(String fileName) throws FileNotFoundException

Java's compiler checks that some thrown exceptions -- those other than RuntimeException and its subclasses -- are either caught or declared thrown. Compilation will fail otherwise. This helps find some errors at compile-time, before the program is ever run.

Java的编译器会检查一些抛出的异常 - 除RuntimeException及其子类之外的异常 - 被捕获或声明抛出。否则编译将失败。这有助于在程序运行之前在编译时发现一些错误。

One option is to declare your calling function to throw the exception or a superclass:

一种选择是声明你的调用函数抛出异常或超类:

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

A better option in this case is to catch the exception and do something with it. For example, here's how you can do that for the Scanner() exception:

在这种情况下,更好的选择是捕获异常并对其执行某些操作。例如,以下是如何为Scanner()异常执行此操作:

    File inFile = new File("C:\\Users\\User\\Selenium\\scrapjv\\interface\\NASDAQlist.txt");
    try {
        Scanner sc = new Scanner( inFile );
        List<String> symbolList = new ArrayList<String>();
        while (sc.hasNextLine()) {
            symbolList.add(sc.nextLine());
        }
    }
    catch ( FileNotFoundException e ) {
        System.out.println("Could not find file: " + inFile.getAbsolutePath());
    }

#2


1  

Your two Scanner declaration lines have a chance to throw an exception, which are basically errors that happen after the code is executed (because of this they are sometimes called runtime errors). Because the compiler knows that your code might make a FileNotFoundException happen, it requires you to catch the exception.

您的两个Scanner声明行有机会抛出异常,这些异常基本上是在执行代码后发生的错误(因此有时称为运行时错误)。因为编译器知道您的代码可能会发生FileNotFoundException,所以它需要您捕获异常。

This is done by enclosing the code in a try-catch block.

这是通过将代码包含在try-catch块中来完成的。

try {

    Scanner sc = new Scanner(new File("C:\\Users\\User\\Selenium\\scrapjv\\interface\\NASDAQlist.txt"));
    List<String> symbolList = new ArrayList<String>();
    while (sc.hasNextLine()) {
        symbolList.add(sc.nextLine());
    }


    PrintWriter logput = new PrintWriter("C:\\Users\\User\\Selenium\\scrapjv\\interface\\log.txt", "UTF-8");
    for (String symb : symbolList) {
        System.out.println(symb);
    }
    logput.close();


} catch (java.io.FileNotFoundException ex)
{
    ex.printStackTrace();
}